Board index » delphi » Add Lookup field to table with dynamic fields.

Add Lookup field to table with dynamic fields.

How can I add a lookup field to a table with dynamic fields ?

I tried: Table1.Fieldlist.Add( 'Name');
but here I got the error: read-only property.

I do not want to use persistent fields because when a new field is added or
the definition of an existing field is changed, I get an error when starting
my app.

Thanks,

Hubert

 

Re:Add Lookup field to table with dynamic fields.


Quote
>How can I add a lookup field to a table with dynamic fields ?

>I tried: Table1.Fieldlist.Add( 'Name');
>but here I got the error: read-only property.

>I do not want to use persistent fields because when a new field is added or
>the definition of an existing field is changed, I get an error when starting
>my app.

You can not have lookup fields with out defining Persistent fields.  You can
define your persistent fields are runtime.

Sample code

var
  f : TField;
  i : integer;
begin
 table1.FieldDefs.Update
  Table1.Close;
  //create persistent fields
  for i := 0 to Table1.FieldDefs.Count - 1 do
   if table1.FindField(table1.FieldDefs[i].Name) = nil then //persistent
                                                        // fielddoes not exist
    Table1.FieldDefs.Items[i].CreateField(Table1);
  f := TStringField.Create(Table1);
  f.Name := 'Table1lookup';
  f.FieldName := 'lookup';
  f.DisplayLabel := 'lookup';
  f.fieldKind :=fklookup;
  f.Calculated := True;
  f.DataSet := Table1;
  f.lookupDataSet:=table2;
  f.Keyfields:= 'Keyfield1';
  f.LookupKeyfields :=  'Keyfield1';
  f.LookupResultField:= 'ResultField';
  Table1.Open;
end;
--
Brian Bushay (TeamB)
Bbus...@NMPLS.com

Re:Add Lookup field to table with dynamic fields.


Thanks, Brian.

Other Threads