Board index » delphi » Adding lookup or calculated fields at runtime

Adding lookup or calculated fields at runtime

Is there a way to add lookup or calculated fields to a table at runtime? The
table does not have any persistent fields.

Regards,
Rob McDonell
ARK ANGLES - Advanced Australian Software
Web: http://www.arkangles.com

 

Re:Adding lookup or calculated fields at runtime


Try the following.
---------------------------------------------------------------------
How do I dynamically create fields from a dataset at runtime?

 var
  I: Integer;
  Field: TField;
 begin
  { Can only add fields to inactive dataset. }
  Table1.Active := False;

  { Allocates field definitions if dataset has never been made active. }
  Table1.FieldDefs.Update;

  { Create all fields from definitions and add to dataset. }
  for I := 0 to Table1.FieldDefs.Count - 1 do
  begin
    { Here is where we actually tell the dataset to allocate a field. }
    { Field gets assigned but we don't need it - just points to new field. }
    Field := Table1.FieldDefs[I].CreateField(Table1);
  end;

  { This is how you can add additional calculated fields }
  Field := TStringField.Create(Table1);
  Field.FieldName := 'Total';
  Field.Calculated := True;
  Field.DataSet := Table1;

  { Now we can see our fields. }
  Table1.Active := True;
---------------------------------------------------------------------

--
Bill
(TeamB cannot answer questions received via email)

Re:Adding lookup or calculated fields at runtime


Thanks Bill, that's just what I needed.

Following your basic procedure I also created Lookup fields like this:

  { This is how you can add additional lookup fields }
  AreaLookupTable := TTable.Create(Self);
  AreaLookupTable.TableName := 'AREA.DBF';
  AreaLookupTable.Open;
  Field := TStringField.Create(Table1);
  Field.Size := 30;
  Field.FieldKind := fkLookup;
  Field.FieldName := 'AREANAME';
  Field.KeyFields := 'AREA';
  Field.LookupKeyFields := 'AREA';
  Field.LookupDataSet := AreaLookupTable;
  Field.LookupResultField := 'AREANAME';
  Field.DataSet := Table1;

Regards,
Rob McDonell
ARK ANGLES - Advanced Australian Software
Web: http://www.arkangles.com

"Bill Todd (TeamB)" <bill.nos...@dbginc.com> wrote in message
news:3b9d482e$1_2@dnews...

Quote
> Try the following.
> ---------------------------------------------------------------------
> How do I dynamically create fields from a dataset at runtime?

>  var
>   I: Integer;
>   Field: TField;
>  begin
>   { Can only add fields to inactive dataset. }
>   Table1.Active := False;

>   { Allocates field definitions if dataset has never been made active. }
>   Table1.FieldDefs.Update;

>   { Create all fields from definitions and add to dataset. }
>   for I := 0 to Table1.FieldDefs.Count - 1 do
>   begin
>     { Here is where we actually tell the dataset to allocate a field. }
>     { Field gets assigned but we don't need it - just points to new
field. }
>     Field := Table1.FieldDefs[I].CreateField(Table1);
>   end;

>   { This is how you can add additional calculated fields }
>   Field := TStringField.Create(Table1);
>   Field.FieldName := 'Total';
>   Field.Calculated := True;
>   Field.DataSet := Table1;

>   { Now we can see our fields. }
>   Table1.Active := True;
> ---------------------------------------------------------------------

> Bill
> (TeamB cannot answer questions received via email)

Other Threads