Calculated Fields

J_Pygram

Member²
Can anyone tell me how to get a TOracleDataSet to trigger the OnCalcFields event when it is created at run time. I have the following in the .h file...

private:
TOracleDataSet *person;
TStringField *personFULL_NAME;
void __fastcall CalcFields(TDataSet *DataSet);

In the .cpp file...

person=new TOracleDataSet(this);
person->OnCalcFields=CalcFields;
.
.
.
personFULL_NAME=new TStringField(this);
personFULL_NAME->Calculated=true;
.
.
.
person->Open(); // this should trigger the CalcFields event.

I also have an implementation od the CalcFields event handler. However - it does not get triggered. Any ideas anyone?

Thanks in anticipation
John
 
Your sample code doesn't show an association betweeen the person dataset and the personFULL_NAME field. The help files do not make it very clear how to do this, but check out the Fields property of TDataSet, the DataSet property of a TField, and TFieldDef::CreateField.
What is probably happening is that due to lack of an association between the field and the dataset, the dataset doesn't think it has any calculated fields. In that case, why bother to call OnCalcFields...
HTH

------------------
Frans
 
Thanks Frans
You are on the right lines but I can not quite get that last mile. Anyone else got any thoughts?

Thanks in advance
John
 
Looking at db.pas I can find 2 requirements for the OnCalcFields event:

1. There must be one or more calculated fields.
2. TOracleDataSet->AutoCalcFields must be True.

------------------
Marco Kalter
Allround Automations
 
To any interested persons...

I seem to have cracked it. The secret is using persistent fields and adding
them to the list of fields in the TTable object. The following seems to work
but I am never too proud to listen to hints or improvements that others may
have.

In the .h file...

private: // User declarations
TOracleDataSet *tPerson;
TStringField *tPersonSURNAME;
TStringField *tPersonFIRSTNAME;
TStringField *tPersonFULL_NAME;

In the .cpp file ...

//-------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
tPerson=new TOracleDataSet(this);
tPerson->DatabaseName="CabConAdmin";
tPerson->TableName="PERSON";
tPerson->OnCalcFields=tPersonCalcFields;
tPerson->AutoCalcFields=true;

TStringField *T1 = new TStringField(NULL);
T1->FieldName = "FIRSTNAME";
T1->Name = tPerson->Name + T1->FieldName;
T1->Index = tPerson->FieldCount;
T1->Size = 20;
T1->DataSet = tPerson;
tPersonFIRSTNAME=T1;

TStringField *T2 = new TStringField(NULL);
T2->FieldName = "SURNAME";
T2->Name = tPerson->Name + T2->FieldName;
T2->Index = tPerson->FieldCount;
T2->Size = 20;
T2->DataSet = tPerson;
tPersonSURNAME=T2;

TStringField *T3 = new TStringField(NULL);
T3->FieldName = "FULL_NAME";
T3->Name = tPerson->Name + T3->FieldName;
T3->Index = tPerson->FieldCount;
T3->Size = 41;
T3->Calculated = true;
T3->DataSet = tPerson;
tPersonFULL_NAME=T3;
tPerson->Open();
DataSource1->DataSet=tPerson;
}
//-------------------------------------------__fastcall TForm1::~TForm1(void)
{
if (tPerson)
delete tPerson;
}
//-------------------------------------------void __fastcall TForm1::tPersonCalcFields(TDataSet *DataSet)
{
tPersonFULL_NAME->AsString=tPersonFIRSTNAME->AsString + " "
+tPersonSURNAME->AsString;
}
//-------------------------------------------

Have a nice day.
John
 
Back
Top