When designing a database, we try to remove some of the redundant data fields. For example, in the staff information base, for the employees in the unit, we only keep the unit number, and the specific units of data stored in a separate unit database, which is in line with the requirements of the standardization of relational databases.
The problem with this is that when editing employee information, most users are unwilling to work with the unit number and want to enter or select the unit name directly, because the unit number is too intuitive and error-prone. Although the SQL language allows you to implement links to two databases in the query control, DELPHI3 provides a shortcut for users to manipulate the unit name directly when editing the employee information base, and to select from a drop-down list that contains all the organization names. This requires a very useful lookup field programming.
When you edit one record at a time in a form and edit multiple records at a time in the DBGrid grid, the lookup field is applied in a very different way, using the Tdblookupcombobox control, which uses the Lookup field field of the table.
The use of a Tdblookupcombobox control
The Tdblookupcombobox control can be linked to two data sources at the same time, one is the primary data source DataSource and the other is a level two data source Listsource. In the primary and level two data sources, you must have exactly the same two fields, called Lookup fields (or key fields), set to Tdblookupcombobox DataField and Keyfield properties, and ListField properties to set display fields. The display field is from the level two data source Listsource.
For example, the employee information base includes fields such as employee number, name, date of birth, sex, origin, unit number, etc., and the unit information base includes the unit number, unit name, name of person in charge, telephone number, fax number, email address, etc. When editing employee information, using the unit name in the unit database to replace a worker's unit number, you need to add a Tdblookupcombobox control object to the form and set its properties:
Datafield=dwbh |
The Unit Number field of the employee information base |
Datasource=datasource1 |
Data source of employee information base |
Keyfield=dwbh |
Unit Number field of the Unit information base |
Listsource=datasource2 |
Data source of the organization information base |
Listfield=dwmc |
Unit Name field in the Unit library for display |
The Run-time interface, as shown in figure one, can be selected from the Drop-down list that is activated by clicking the down arrow to the right of the Unit name edit box.
Figure I