Board index » delphi » Adding calculated fields programatically

Adding calculated fields programatically

Hi there,

I just cain't figure-out how ot add a calculated field to a TTable
component at run-time. I've tried setting the table object as the owner
of the calculated field ( NewField := TStringField.Create(TheTable) )
and I've tried adding the field via the FieldDefs property (
TheTable.FieldDefs.Add(...) ). I must be pretty far off!!

Can someone hint me into the right direction, please??

Thank you all!!

Oliver.

 

Re:Adding calculated fields programatically


Quote
>I just cain't figure-out how ot add a calculated field to a TTable
>component at run-time. I've tried setting the table object as the owner
>of the calculated field ( NewField := TStringField.Create(TheTable) )
>and I've tried adding the field via the FieldDefs property (
>TheTable.FieldDefs.Add(...) ). I must be pretty far off!!

here is an example.  but remember you still have to have code in the
OnCalcFields event of the ttable to use it.

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...@DataGuidance.com

Other Threads