Board index » delphi » Add calculated field at run-time

Add calculated field at run-time

D5 Enterprise / MS SQL 7

I am using the same TADOQuery component to look at more than one table (only
one at a time)
so the SQL is built as needed. They have different fields, so persistent
fields are not being used.
When a certain check box is checked, I want to show an extra calculated
field.
I tried the sample code under TFloatField.Create in the docs, but can't get
it to see/display
the field.

1. Do I need to dynamically add all fields as persistent TField objects to
make this work?
2. How do I set it to calculated in code?  The docs say this property is
read-only.

 

Re:Add calculated field at run-time


Quote

>1. Do I need to dynamically add all fields as persistent TField objects to
>make this work?

Yes

Quote
>2. How do I set it to calculated in code?  The docs say this property is
>read-only.

here is an example
var
  f : TField;
  i : integer;
begin
 table1.FieldDefs.Update
  Table1.Close;
  for i := 0 to Table1.FieldDefs.Count - 1 do
   //create persistent field that do not exist
   if table1.FindField(table1.FieldDefs[i].Name) = nil then
      table1.FieldDefs.Items[i].CreateField(Table1);
//create a calculated field
  f := TStringField.Create(Table1);
  f.Name := 'Table1CalcField';
  f.FieldName := 'CalcField';
  f.DisplayLabel := 'CalcField';
  f.Calculated := True;
  f.DataSet := Table1;
  Table1.Open;
end;
--
Brian Bushay (TeamB)
Bbus...@NMPLS.com

Re:Add calculated field at run-time


Thanks Brian.

"Brian Bushay TeamB" <BBus...@Nmpls.com> wrote in message
news:g8e45ugid78ilijtkf5uu5ngcnbrjugo1h@4ax.com...

Quote

> >1. Do I need to dynamically add all fields as persistent TField objects
to
> >make this work?
> Yes

> >2. How do I set it to calculated in code?  The docs say this property is
> >read-only.
> here is an example
> var
>   f : TField;
>   i : integer;
> begin
>  table1.FieldDefs.Update
>   Table1.Close;
>   for i := 0 to Table1.FieldDefs.Count - 1 do
>    file://create persistent field that do not exist
>    if table1.FindField(table1.FieldDefs[i].Name) = nil then
>       table1.FieldDefs.Items[i].CreateField(Table1);
> file://create a calculated field
>   f := TStringField.Create(Table1);
>   f.Name := 'Table1CalcField';
>   f.FieldName := 'CalcField';
>   f.DisplayLabel := 'CalcField';
>   f.Calculated := True;
>   f.DataSet := Table1;
>   Table1.Open;
> end;
> --
> Brian Bushay (TeamB)
> Bbus...@NMPLS.com

Other Threads