How add calculated fields to a table at runtime? lazarus / 16-Databases ---------------------------------------------------------------------- gato2707 · 2013-05-27 05:11 Hi everybody: Are there a way to add calculated fields to a table at runtime? It's simple when the FieldDefs was created at design time, but in this way the table becomes into an static structure inside the Object Pascal code. I would like to create the query (Table) at runtime because the phisycal structure of a related table may change in time. v.g. The table used for today may have a simple structructe like: id INTEGER, Name VARCHAR(20), CodeID CHAR(10) But maybe nex time the program need access a different phisycal table like: id INTEGER, Name VARCHAR(20), SPEC INTEGER, CodeID CHAR(10) Or any other number of fields or kind of them I try with the FieldDefs.CreateField samples found at Internet, but all of them are related to Delphi who has a very different definition for that method. In Lazarus/fpc (http://lazarus- ccr.sourceforge.net/docs/fcl/db/tfielddef.createfield.html) its method only has a single argument ---------------------------------------------------------------------- gato2707 · 2013-05-29 15:39 Hi everybody: I have been solved the way to create Calculated Fields in runtime. I hope this solution be useful for any who has same need in the future. 1) Create a proyect and add every unit you'll need to work wit a database. 2) Add these two variables as global to the unit. I suppose you can declare them as part of the unit class. Code: [Select]var  Form1: TForm1;  MyFields: array of TField;  Temf: TStringField; implementation {$R *.lfm} { TForm1 } 3) In the OnCreate event do next: Code: [Select]procedure TForm1.Create(Sender: TObject); var  i:integer; begin //Create your db componentes as you needd //You need open an then close the table for load the fields  MyQ.Open;  MyQ.Close;  SetLength(MyFields, MyQ.FieldDefs.Count); //Add one by one the fields to make them accesible.  for i:=0 to MyQ.FieldDefs.Count - 1 do    MyFields[i] := MyQ.FieldDefs[i].CreateField(MyQ); //Create all Calculated Fields as you need  Temf := TStringField.Create(nil);  Temf.FieldKind := fkCalculated;  Temf.FieldName := 'NewCalcField';  Temf.DataSet := MyQ;  MyQ.Open; end; 4) OnDestroy event be sure to free all fields you create before Code: [Select]procedure TForm1.FormDestroy(Sender: TObject); var  i: Integer; begin  for I := low(MyFields) to High(MyFields) do    MyFields[i].Free;  SetLength(MyFields, 0);  Temf.Free;  MyQ.Close; end;