KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Filling Combo Boxes With The Result Of A Query
PRODUCT: 4D | VERSION: 6.8 | PLATFORM: Mac & Win
Published On: April 18, 2003
Your current browser may not allow you to download the asset. We recommend Mozilla Firefox or Google Chrome.
Log In

Summary:
This tech tip shows how to display the values from a field for a selection records returned by a query in a combo box.

Suppose you have the following interface and you want to display the detail of one specific invoice for the current customer. Notice the Combo box in the upper left of the form. It has been assigned a default value of the invoice number of one of the invoices belonging to this customer.

combo box

Clicking on the combo box displays a list of all the invoice numbers of invoices belonging to this customer.

invoices belonging to customer

Below you see the combo box with the user selected invoice number displayed in the combo box enterable area:

combo box enterable area

When the user tabs out of the combo box enterable area, the specified invoice is displayed:

invoice displayed

This was implemented as follows:


  1. Place a combo box on the form.

  2. Specify the combo box variable name. Here, aLInvoiceNbs was used.

  3. Specify the combo box data type. Here, numeric was used because the source field is a Long Integer data type.

  4. Turn on the following events for the combo box object: On Load, On Data Change. Code assigned to the On Load event will initialize the combo box. Code assigned to the On Data Change event will execute when the user tabs out of the combo box enterable area, if they have changed the data in that area by typing or selecting a value form the combo box pop-up list.

  5. Turn off all other events.

  6. Place the following code in the object method of the combo box object:


    Case of
      : (Form event=On Load )

       `Search for invoices for the current customer
       QUERY([Invoices];[Invoices]CustomerID=[Customers]CustomerID)
       `Load the invoice numbers into an array
       SELECTION TO ARRAY([Invoices]InvoiceNo;aLInvoiceNbs)
       `Sort the array of invoice numbers ascending
       SORT ARRAY(aLInvoiceNbs;>)
       `Assign the value in the first element of the array to be the default value
       `displayed in the enterable area of the combo box.
       aLInvoiceNbs{0}:=aLInvoiceNbs{1}

       `When the user tabs out of the enterable area of the combo box.
     : (Form event=On Data Change )
       `Find and load the record selected by the user
       QUERY([Invoices];[Invoices]InvoiceNo=aLInvoiceNbs{0})
       `Display the user selected record
       MODIFY RECORD([Invoices])
    End case


  7. Notice that the array and the combo box must have the same name for the contents of the array to display in the combo box.