Page 1 of 1

Adding Calc Fields at runtime

Posted: Wed 16 May 2007 13:11
by sological
What is the best way to add fields (Calc fields) at runtime, (ie not using the fields editor in IDE)?

Posted: Thu 17 May 2007 07:55
by Antaeus
This question concerns common issues of using TDataSet. You can read more about this in the Delphi help.
In your case you can use something like this:

Code: Select all

  var
  f: TField;
  i: integer;
  ...

  MyQuery.FieldDefs.Update;
    for i := 0 to MyQuery.FieldDefs.Count - 1 do
      f := MyQuery.FieldDefs[i].CreateField(MyQuery);

    f := TFloatField.Create(MyQuery);
    f.FieldName := 'Calc';
    f.Calculated := True;
    f.DataSet := MyQuery;

    MyQuery.Open;

Posted: Mon 21 May 2007 05:15
by sological
Antaeus,

Thank you very much for your reply it has helped point me in the right direction.

Have a great day
Ray