Adding Calc Fields at runtime

Discussion of open issues, suggestions and bugs regarding MyDAC (Data Access Components for MySQL) for Delphi, C++Builder, Lazarus (and FPC)
Post Reply
sological
Posts: 2
Joined: Wed 16 May 2007 12:41

Adding Calc Fields at runtime

Post by sological » Wed 16 May 2007 13:11

What is the best way to add fields (Calc fields) at runtime, (ie not using the fields editor in IDE)?

Antaeus
Posts: 2098
Joined: Tue 14 Feb 2006 10:14

Post by Antaeus » Thu 17 May 2007 07:55

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;

sological
Posts: 2
Joined: Wed 16 May 2007 12:41

Post by sological » Mon 21 May 2007 05:15

Antaeus,

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

Have a great day
Ray

Post Reply