Board index » delphi » Calculated Field

Calculated Field

I want to add calculated field using code (not Field Editor)

Waht must I to do ?

Thanks !

Jean-Pascal Laux

 

Re:Calculated Field


|>I want to add calculated field using code (not Field Editor)
|>
|>Waht must I to do ?
|>
|>Thanks !
|>
|>
|>Jean-Pascal Laux

Try this
type
    procedure TAmountCalc(DataSet: TDataset);

procedure TForm1.FormCreate(Sender: TObject);
var
  MyTable: TTable;
  MyDataSource: TDataSource;
  MyGrid: TDBGrid;
begin
  MyTable := TTable.Create(Self);
  with MyTable do
  begin
    DatabaseName := 'DBDemos';
    TableName := 'Test.DB';
    OnCalcFields := TAmountCalc;
    with FieldDefs do
    begin
      Add('ItemsTotal', ftCurrency, 0, false);
      FieldDefs[0].CreateField(MyTable);
      Add('TaxRate', ftFloat, 0, false);
      FieldDefs[1].CreateField(MyTable);
      TFloatField(Fields[1]).DisplayFormat := '##.0%';

    { Create a calculated TField, assign properties,
      and add to MyTable's field definitions array. }
      TAmount := TFloatField.Create(MyTable);
      with TAmount do
      begin
        FieldName := 'TAmount';
        Calculated := True;
        Currency := True;
        DataSet := MyTable;
        Name := MyTable.Name + FieldName;
        MyTable.FieldDefs.Add(Name, ftFloat, 0, false);
      end;
    end;
    MyTable.CreateTable;
  end;
  MyDataSource := TDataSource.Create(Self);
  MyDataSource.DataSet := MyTable;
  MyTable.Active := True;
end;

Re:Calculated Field


Quote
J-P Laux wrote:

> I want to add calculated field using code (not Field Editor)

> Waht must I to do ?

> Thanks !

> Jean-Pascal Laux

 Visit Borland tehnical information page:
http://www.borland.com/devsupport/delphi/ti_list/

and there is a line No. TI3171 which will help to solve your problem

Marjan jesenovec

Other Threads