Displaying and Editing Data in Lookup List and Combo Boxes
Go Up to Displaying a Single Record
Lookup list boxes and lookup combo boxes (Vcl.DBCtrls.TDBLookupListBox and Vcl.DBCtrls.TDBLookupComboBox) present the user with a restricted list of choices from which to set a valid field value. When a user selects a list item, the corresponding field value is changed in the underlying dataset.
For example, consider an order form whose fields are tied to the OrdersTable. OrdersTable contains a CustNo field corresponding to a customer ID, but OrdersTable does not have any other customer information. The CustomersTable, on the other hand, contains a CustNo field corresponding to a customer ID, and also contains additional information, such as the customer's company and mailing address. It would be convenient if the order form enabled a clerk to select a customer by company name instead of customer ID when creating an invoice. A TDBLookupListBox that displays all company names in CustomersTable enables a user to select the company name from the list, and set the CustNo on the order form appropriately.
These lookup controls derive the list of display items from one of two sources:
A lookup field defined for a dataset. To specify list box items using a lookup field, the dataset to which you link the control must already define a lookup field.
To specify the lookup field for the list box items
- Set the DataSource property of the list box to the data source for the dataset containing the lookup field to use.
- Choose the lookup field to use from the drop-down list for the DataField property.
- When you activate a table associated with a lookup control, the control recognizes that its data field is a lookup field, and displays the appropriate values from the lookup.
A secondary data source, data field, and key. If you have not defined a lookup field for a dataset, you can establish a similar relationship using a secondary data source, a field value to search on in the secondary data source, and a field value to return as a list item.
To specify a secondary data source for list box items
- Set the DataSource property of the list box to the data source for the control.
- Choose a field into which to insert looked-up values from the drop-down list for the DataField property. The field you choose cannot be a lookup field.
- Set the ListSource property of the list box to the data source for the dataset that contain the field whose values you want to look up.
- Choose a field to use as a lookup key from the drop-down list for the KeyField property. The drop-down list displays fields for the dataset associated with data source you specified in Step 3. The field you choose need not be part of an index, but if it is, lookup performance is even faster.
- Choose a field whose values to return from the drop-down list for the ListField property. The drop-down list displays fields for the dataset associated with the data source you specified in Step 3.
When you activate a table associated with a lookup control, the control recognizes that its list items are derived from a secondary source, and displays the appropriate values from that source.
To specify the number of items that appear at one time in a TDBLookupListBox control, use the RowCount property. The height of the list box is adjusted to fit this row count exactly.
To specify the number of items that appear in the drop-down list of TDBLookupComboBox, use the DropDownRows property instead.
Note: You can also set up a column in a data grid to act as a lookup combo box. For information on how to do this, see Defining a Lookup List Column.