user1769184
user1769184

Reputation: 1621

add calcfield to TQuery

I found this code to create a calc field to a TADOTable in Delphi somewhere ...

    .....
    procedure TfrmMain.ABSTable1CalcFields(DataSet: TDataSet);
    begin
      with ABSTable1 do
       FieldByName('cost').AsFloat := FieldByName('price').AsFloat *
                                      FieldByName('quantity').AsInteger;
       //  add new field cost as Price * quantity  !!!!
    end;

    end.

Inside my app i create a TADOQuery at rum time like

  try
    Fquery.sql.clear;
    Fquery.sql.AddStrings(Amemo.lines);
    Fquery.Open;

    .....    
  finally

  end;

How to add more calc fields to my Query derived from the first code Fragment ?

Upvotes: 0

Views: 544

Answers (1)

MartynA
MartynA

Reputation: 30715

I think the only way you can easily do this is by creating a set of persistent TFields in the IDE (or do the equivalent creation of them in code before you open the dataset). Otherwise, when you call Open on the dataset, IIRC that will call BindFields and that - unless the dataset already has a set of TFields - will create a set of dynamic TFields which will last as long as the dataset is open, but will not include any calculated fields.

By the time BindFields has been called, it's too late to add any more, so you have to set them up beforehand or not at all.

Upvotes: 3

Related Questions