Lookup with DBLookup ComboBox

As stated above, when your data entry form is made of more data controls (DBEdit, DBComboBox, etc.) it makes sense to just use DBLookupComboBox without creating a new field.

For the start, use dragging from the Fields editor to add data controls to a form. Drag Name, Author, Type and Description. This will add 4 DBEdit components and 4 Label components. At this point, remove the DBEdit connected with the Author field of the Applications table and replace it with a DBLookupComboBox. Name it ApplicationsAuthorLookup. We'll also need another DataSource component. Drop one on the form, change the name to AuthorsSource and link it to AuthorsTable. Finally, we need to set the lookup combo box to work properly. Use Object Inspector and set the following:

object ApplicationsAuthorLookup: TDBLookupComboBox DataSource = ApplicationsSource DataField = 'Author' ListSource = AuthorsSource KeyField = 'authorname' ListField = 'authorname,-email' end

These properties are key to the lookup connection:

  • DataSource and DataField determine the main connection. The DatField is a field into which we insert the looked-up values.
  • ListSource is the source of the lookup dataset.
  • KeyField identifies the field in the ListSource that must match the value of the DataField field. . ListFields is the field (one or more) of the lookup dataset that are actually displayed in the combo. ListField can show more than one field. Multiple field names should be separated by semicolons. You have to set large enough value for the DropDownWidth (of a ComboBox) to really see multiple columns of data.

Just for practice, replace the Type's field DBEdit with DBLookComboBox and set a lookup relation with the Types table. Note that you need one more DataSource and one more ADO Table pointing to the Types table.

Set the DataSource and DataField properties to the dataset and field where the selection will be written (Applications; Type). Set the ListSource, KeyField and ListField properties to the lookup field from which the list should be populated (Types; TypeName, TypeName).

Graphically, connections between tables, sources and ADOConnections can now be presented as:

That's it. Again, no code required. When the user selects an item from the combo box, an appropriate value of the KeyField field changes the value of the DataField. At run time, the form looks like:

Dblookupcombobox Delphi

Note 1: When you want to display more than one field in a LookupComboBox, as in the example above, you have to make sure that all columns are visible. This is done by setting the DropDownWidth property. However, you'll see that initially you have to set this to a very large value which results in dropped list being too wide in most cases. One workaround is to set the DisplayWidth of a particular Field shown in a drop down list. The next code snippet, placed inside the OnCreate event for the form, ensures that both author name and it's email are displayed inside the drop down list. AuthorsTable.FieldByName('authorname').DisplayWidth:=15; AuthorsTable.FieldByName('email').DisplayWidth:=20;

Note 2 : If you drag AEmail field from a Field Editor to a form, Delphi will connect it with a DBLookupComboBox automatically. Of course, key lookup properties will look different since we don't need another data source for this link to work - lookup field is already defined "inside" the ApplicationsTable/Source.

+8 -1

Responses

  • kristin
    How to put dblookup combobox delphi database acces?
    8 years ago
  • omar
    Why is a DBLookupComboBox displaying a whole table instead of just a field?
    8 years ago
  • selassie
    How to lookup combined fields in dblookupcombobox in Delphi?
    8 years ago
  • Franca
    How to prevent typing in LookupCombo in delphi?
    8 months ago

Post a comment