Technical Information Database TI4548D.txt - Creating Lookup Fields at Runtime Category :Database Programming Platform :All-32Bit Product :All32Bit, Description: Creating a lookup field at runtime differs in only a few ways from creating any standard TField object. (for more explicit information on this topic and other related topics see the TI entitled "Dynamically creating a TTable & fields at runtime".) Three properties of a field that must be set in order to function as a Lookup field are LookUpDataset, LookUpKeyFields and LookUpResultField. The sample code below demonstrates how to create a Lookup field at runtime using the two DBDEMOS tables 'customer.DB' and 'orders.DB'. Start a new application (File|New Application). Drop two TTables, a TButton, a TDBLookupComboBox and a TDataSource on a form. Set the two TTables 'DatabaseName' property to 'DBDEMOS'. Set Table1's 'TableName' property to 'CUSTOMER.DB' and Table2's 'TableName' property to 'ORDERS.DB'. Right click on the TTable object for Table2 and launch the field editor. Add the 'CustNo' field object (right click on the fields editor and choose 'Add Fields', then select 'CustNo' from the list). Copy and paste the code below into the code editor. Example: unit Unit1; interface uses Forms, Classes, Controls, StdCtrls, Db, DBTables, DBCtrls; type TForm1 = class(TForm) Table1: TTable; // DBDemos customer table Table2: TTable; // DBDemos orders table Button1: TButton; DBLookupComboBox1: TDBLookupComboBox; DataSource1: TDataSource; Table2CustNo: TFloatField; // CustNo key field object used // for Lookup procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.DFM} procedure TForm1.Button1Click(Sender: TObject); begin with TStringField.Create(Table2) do begin FieldName := 'MyLookup'; FieldKind:= fkLookup; DataSet := Table2; Name := Dataset.Name + FieldName; KeyFields:= 'CustNo'; LookUpDataset:= Table1; LookUpKeyFields:= 'CustNo'; LookUpResultField:= 'Company'; DbLookupCombobox1.DataField:= FieldName; DataSource1.DataSet:= Dataset; Table2.FieldDefs.Add(Name, ftString, 20, false); end; DbLookupCombobox1.DataSource:= Datasource1; Table1.Active:= True; Table2.Active:= True; end; end. Reference: 4/22/99 12:29:17 PM
Last Modified: 01-SEP-99