Board index » delphi » Add calculated fields to TQuery at runtime?

Add calculated fields to TQuery at runtime?

Is is possible to add calculated fields to a TQuery at runtime?  I am adding
the fields to the TQuery in the AfterOpen event like so:

 for i := 0 to FBrowseProps.CalculatedColumns.Count - 1 do begin
  aField := TStringField.Create ( Query );
  aField.FieldName := FBrowseProps.CalculatedColumns [ i ];
  aField.Calculated := True;
  aField.Size := 10;
  aField.Visible := True;
  Query.Fields.Add ( aField );
 end;

AutoCalcFields is set to true and the OnCalcFields event is assigned at
design time.  However, the OnCalcFields event is never called.

 

Re:Add calculated fields to TQuery at runtime?


You can add calculated fields to a TQuery at runtime.  I'm guessing the
problem is when you create TFields.  Try creating them before you open the
query.  I've created calculated fields on a TQuery at runtime and they
worked just fine.

Quote
"Bill Fugina" <b...@dogspots.com> wrote in message

news:39e5e86c$1_2@dnews...
Quote
> Is is possible to add calculated fields to a TQuery at runtime?  I am
adding
> the fields to the TQuery in the AfterOpen event like so:

>  for i := 0 to FBrowseProps.CalculatedColumns.Count - 1 do begin
>   aField := TStringField.Create ( Query );
>   aField.FieldName := FBrowseProps.CalculatedColumns [ i ];
>   aField.Calculated := True;
>   aField.Size := 10;
>   aField.Visible := True;
>   Query.Fields.Add ( aField );
>  end;

> AutoCalcFields is set to true and the OnCalcFields event is assigned at
> design time.  However, the OnCalcFields event is never called.

Re:Add calculated fields to TQuery at runtime?


Here's what I did and it seems to work well.
Notes:  Opening is global boolean.  CalculatedColumns is a TStringList
containing names of calculated columns.

procedure TSimpleBrowse.QueryAfterOpen(DataSet: TDataSet);
var
 i : Integer;
 aField : TField;
begin
 if not Opening then begin
  Opening := True;
  Query.Close;
  for i := 0 to Query.FieldDefs.Count - 1 do begin
   aField := Query.FieldDefs [ i ].CreateField ( Query );
  end; // for
  for i := 0 to CalculatedColumns.Count - 1 do begin
   aField := TStringField.Create ( Query );
   aField.FieldName := CalculatedColumns [ i ];
   aField.Calculated := True;
   aField.Size := 10;
   aField.Visible := True;
   aField.DataSet := Query;
  end;
  Query.FieldDefs.Update;
  Query.Open;
  //Opening := False; // Can't change fields at runtime unless you set
Opening to False here
 end;
end;

Quote
"Blaine Whittle" <blai...@aproposretail.com> wrote in message

news:39e5f0d6_1@dnews...
Quote
> You can add calculated fields to a TQuery at runtime.  I'm guessing the
> problem is when you create TFields.  Try creating them before you open the
> query.  I've created calculated fields on a TQuery at runtime and they
> worked just fine.

> "Bill Fugina" <b...@dogspots.com> wrote in message
> news:39e5e86c$1_2@dnews...
> > Is is possible to add calculated fields to a TQuery at runtime?  I am
> adding
> > the fields to the TQuery in the AfterOpen event like so:

> >  for i := 0 to FBrowseProps.CalculatedColumns.Count - 1 do begin
> >   aField := TStringField.Create ( Query );
> >   aField.FieldName := FBrowseProps.CalculatedColumns [ i ];
> >   aField.Calculated := True;
> >   aField.Size := 10;
> >   aField.Visible := True;
> >   Query.Fields.Add ( aField );
> >  end;

> > AutoCalcFields is set to true and the OnCalcFields event is assigned at
> > design time.  However, the OnCalcFields event is never called.

Re:Add calculated fields to TQuery at runtime?


Hi,
You already noticed you can't add fields while dataset is opened. So, create
the fields in BeforeOpen event:
begin
  if Query.FieldDefs.Count=0 then Query.FieldDefs.Update;
  if Query.FieldCount=0 then begin
    for i := 0 to Query.FieldDefs.Count - 1 do
      Query.FieldDefs[I].CreateField (self);
    {now create the calculated fields}
  end;
end;

The check for Query.FieldCount=0 is necessary so it does not try to create
fields on reopen (refresh) and you don't need a global variable Opening,
because the code is executed before opening.
--
----------------------
Regards
Robert Cerny
Remove both qwe when replying
email: robert.qwe.ce...@neosys.xrs.qwe.si

No questions via email, unless explicitly invited.

Other Threads