Board index » delphi » Add calculated field in runtime

Add calculated field in runtime

Can I add a calculated field in runtime?
When I try to do it only appear the calculated field, how can I do?
This is my code: (c++Builder)

    Table1->Active=false;
    StringField *f;
    f=new TStringField(Table1);
    f->FieldName="Name";
    f->DataSet=Table1;
    f->Size=40;
    f->DisplayLabel="Name";
    f->DisplayWidth=40;
    f->FieldKind=fkCalculated;
    f->Name="Table1Name";
    f->Index = Table1->FieldCount;  <= It's 0 because table is closed
    f->Visible=true;
    Table1->FieldDefs->Update();
    Table1->Active=true;

extract from http://community.borland.com/article/0,1410,15707,00.html

procedure TForm1.Button2Click(Sender: TObject);
var
  T: TStringField;
begin
  Query1.Close;
  T := TStringField.Create(Self);
  T.FieldName := 'CO_NAME';
  T.Name := Query1.Name + T.FieldName;
  T.Index := Query1.FieldCount;
  T.DataSet := Query1;
  Query1.FieldDefs.UpDate;
  Query1.Open;
end;

 

Re:Add calculated field in runtime


You can add a calculated field at runtime but the OnCalcFields event handler
must already exist in your program.

--
Bill
(TeamB cannot answer questions received via email)

Re:Add calculated field in runtime


Ok, but I have OnCalcFields event handler already.
The problem is that the above code doesnt work properly, only appears the
calculated field, the table physical fields doesn't appears.
And I can't use persistant fields.

Sorry about my english

"Bill Todd (TeamB)" <bill.nos...@dbginc.com> escribi en el mensaje
news:3be86cce$1_2@dnews...

Quote
> You can add a calculated field at runtime but the OnCalcFields event
handler
> must already exist in your program.

> --
> Bill
> (TeamB cannot answer questions received via email)

Re:Add calculated field in runtime


You have to use persistent fields to use a calculated field. You must either
create them at design time or runtime.

--
Bill
(TeamB cannot answer questions received via email)

Re:Add calculated field in runtime


Thank you, now works but I have another question.

I have to use inheritsfrom to know the field kind then create a persistent
field (TStringfield, TIntegerField, ...) but I have to copy the properties
manually and different kind of field have different properties.

 How can I copy a field from a physical table creating a persistent field in
runtime?

Sorry, be patient.

Thanks in advance.

"Bill Todd (TeamB)" <bill.nos...@dbginc.com> escribi en el mensaje
news:3be9d7d1_1@dnews...

Quote
> You have to use persistent fields to use a calculated field. You must
either
> create them at design time or runtime.

> --
> Bill
> (TeamB cannot answer questions received via email)

Other Threads