creating calculated field at runtime?

koadam

Member²
Hi,

Can someone show me how can I create a calculated field at runtime with a TOracleDataSet?
The sql is constructed also at runtime, so I *have to create the calc field at runtime, too.
I tried the following code:
f:=TIntegerField.Create(ods1);
f.FieldName:='blabla';
f.FieldKind:=fkCalculated;
f.DataSet:=ods1;
f.Name := f.Dataset.Name + f.FieldName;
ods1.FieldDefs.Add(f.name, f.DataType, f.Size, f.Required);
ods1.Open;
And seen no calc field, only the real fields...

Thanks in advance (for any info),
Koadam
 
I'm not sure if this is possible. You can always perform the calculation in the SQL of course. If the dataset is updatable, set TOracleDataSet.OracleDictionary.FieldKinds to True, and TOracleDataSet.RefreshOptions to [roAfterInsert, roAfterUpdate], so that the calculated fields are read-only and automatically refreshed after posting a record.

------------------
Marco Kalter
Allround Automations
 
I could not believe the 'I don't know if this is possible' as in Delphi, anything possible at design-time is also possible at run-time.

So, created a project with an OracleSession, OracleDataSource, DataSet and DBGrid.

Added the following to a button and it works (is identical to the code initially posted except the FieldDefs.Add call was not necessary but also does no harm):

procedure TForm1.Button1Click(Sender: TObject);
var
fld : TIntegerField;
begin
OracleDataSet1.Close;

fld:=TIntegerField.Create(OracleDataSet1);
fld.FieldName:='blabla';
fld.FieldKind:=fkCalculated;
fld.DataSet:=OracleDataSet1;
fld.Name := fld.Dataset.Name + fld.FieldName;
fld.OnGetText := TestGetText;
OracleDataSet1.Open;
end;
 
yes, it works.
**But if you look at your dbgrid, the only field shown in the grid is the "blabla" field.
That's my problem
frown.gif
 
The problem is with the Fields set up in the OracleDataSet. If no fields are setup then you see all from the query BUT as soon as you define a field (in this case the calculated one) then only those defined are seen.

So, since you are adding a field you will need to add other other fields that you want to see in the grid.
 
yes, that right. But, The problem is that you can add field only to a closed/inactive dataset. So, before opening the query I know nothing about the fields after opening.
 
Back
Top