Link to home
Start Free TrialLog in
Avatar of djadja
djadja

asked on

How do I create a calculated field at RUNTIME?

I can't afford the luxury of right clicking on the TTable and adding ALL fields and then adding my calculated field as this gives problems @ runtime when field names/types change.  I want to create a calculated field and a lookup field @ runtime.  How do I go about this?
ASKER CERTIFIED SOLUTION
Avatar of Alone
Alone

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Alone
Alone

Try this:

type
  TMyForm = class(TForm)
    Table1: TTable;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    MyStringField: TStringField;
    MyFloatField: TFloatField;
  end;

{ TMyForm }

procedure TMyForm.Button1Click(Sender: TObject);
begin
  Table1.Close;
  MyStringField := TStringField.Create(Self);
  with MyStringField do
  begin
    FieldKind := fkCalculated;
    FieldName := 'MyString';
    Size := 100;
    DataSet := Table1;
  end;
  MyFloatField := TFloatField.Create(Self);
  with MyFloatField do
  begin
    FieldKind := fkCalculated;
    FieldName := 'MyFloat';
    DisplayFormat := '000000.00';
    DataSet := Table1;
  end;
  Table1.Open;
end;
Sorry my message sent twice :-((
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Accept Answer from Alone

Please leave any comments here within the next seven days.
 
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
 
Paul (pnh73)
EE Cleanup Volunteer