Board index » delphi » Add calculated field to TQuery/TTable?

Add calculated field to TQuery/TTable?

Is there any way that I can add calculated field to an existing TTable
dynamically(in code)? I know that it can be accomplished in design
stage.
 

Re:Add calculated field to TQuery/TTable?


On Fri, 14 Apr 2000 09:02:31 -0400, Sims <s...@fenix2.dol-esa.gov>
wrote:

Quote
>Is there any way that I can add calculated field to an existing TTable
>dynamically(in code)? I know that it can be accomplished in design
>stage.

Something like:
    Field := TStringField.Create(Table1);
    Field.FieldName := 'TempField';
    Field.Size := 4;
    Field.FieldKind := fkCalculated;
    Field.DataSet := Table1;

Execute this code before you open the table and attach an OnCalcFields
event handler where you assign the calculated value for the field.
Also important is that you should have persistent fields or create the
other fields persistantly at runtime.

HTH,

Jan

Re:Add calculated field to TQuery/TTable?


Quote
>Is there any way that I can add calculated field to an existing TTable
>dynamically(in code)? I know that it can be accomplished in design
>stage.

Yes here is an example

  Table1.Active:=false; // Must be false to add Fields to the Dataset
  Table1.FieldDefs.Update; // Make sure we have the latest Fields
  Monthly:=TFloatField.Create(Table1);
  Monthly.FieldName:='Monthly Salary';
  Monthly.Calculated:=true;
  Monthly.Dataset:=Table1;
  Monthly.DisplayFormat:='$#,0.00';
  Table1.Active:=true;

--
Brian Bushay (TeamB)
Bbus...@NMPLS.com

Other Threads