Mirror

How to create a lookup table (Views: 100)


Problem/Question/Abstract:

I have written some code for creating a table and I also want to specify a lookup table for a some fields. What's the code for that?

Answer:

var
  f: TField;
  i: integer;
begin
  table1.FieldDefs.Update
    table1.Close;
  for i := 0 to table1.FieldDefs.Count - 1 do
    if table1.FindField(Query.FieldDefs[i].Name) = nil then
      {persistent field does not exist}
      table1.FieldDefs.Items[i].CreateField(table1);
  f := TStringField.Create(table1);
  f.Name := 'table1lookup';
  f.FieldName := 'lookup';
  f.DisplayLabel := 'lookup';
  f.fieldType := fklookup;
  f.Calculated := True;
  f.DataSet := table1;
  f.lookupDataSet := table2;
  f.Keyfields := 'Keyfield1';
  f.Lookupfields := 'Keyfield1';
  f.LookupResultField := 'ResultField';
  table1.Open;
end;

<< Back to main page