Lookup Fields

As an alternative to placing a DBLookupComboBox component in a form (discussed earlier in this chapter under "Using Lookup Controls"), we can also define a lookup field, which can be displayed with a drop-down lookup list inside a DBGrid component. We've seen that to add a fixed selection to a DBGrid, we can simply edit the PickList subproperty of the Columns property. To customize the grid with a live lookup, instead, we have to define a lookup field using the Fields editor.

As an example, I've built the FieldLookup program, which has a grid displaying orders with a lookup field to display the name of the employee who took the order, instead of the code number of this employee. To accomplish this, I added to the data module a Table component referring to the EMPLOYEE.DB table. Then I opened the Fields editor for the ORDERS table and added all the fields. I selected the EmpNo field and set its Visible property to False, to remove it from the grid (we cannot remove it altogether, because it is used to build the cross-reference with the corresponding field of the EMPLOYEE table).

Now it is time to define the lookup field. If you've followed the preceding steps, you can use the Fields editor of the ORDERS table and select the New Field command, obtaining the New Field dialog box. (As an alternative in Delphi 5, it's possible to use the Diagram page of the editor, drop the two tables there, and drag a lookup relation from the ORDERS table to the EMPLOYEE table, connecting the two in the resulting New Field dialog box. In Delphi 6, though, the lookup relation button is still part of the Diagram page but doesn't seem to be working at all.)

The values you specify in the New Lookup Field dialog box will affect the properties of a new TField added to the table, as demonstrated by the DFM description of the field:

object Table2Employee: TStringField FieldKind = fkLookup FieldName = 'Employee' LookupDataSet = Table2 LookupKeyFields = 'EmpNo' LookupResultField = 'LastName' KeyFields = 'EmpNo' Size = 30 Lookup = True end

This is all that is needed to make the drop-down list work (see Figure 13.10) and to view the value of the cross-references field at design time, too. Notice that there is no need to customize the Columns property of the grid, because the drop-down button and the value of seven rows are taken by default. This doesn't mean you cannot use this property to further customize these and other visual elements of the grid.

FIGURE 13.10:

The output of the Field-Lookup example, with the drop-down list inside the grid displaying values taken from another database table

FIGURE 13.10:

The output of the Field-Lookup example, with the drop-down list inside the grid displaying values taken from another database table

Was this article helpful?

+2 0
Self Publishing a Book The Easy Way

Self Publishing a Book The Easy Way

This comprehensive guide will present you with a variety of self-publishing options and explore their viability. Well take a look at all types of books and book-delivery systems.

Get My Free Ebook


Post a comment