Board index » delphi » Calculated fields - how to add at runtime?

Calculated fields - how to add at runtime?

I have a table (Advantage TDataset descendant) where the physical
table may vary (part of a table viewer).  Therefore, I can't add
persistent fields at design time.  This also means I can't add
calculated fields at design time (via the Fields Editor).

I want to add a calculated field (Deleted status, via an Advantage
method) to the table when it's opened.  I know how to do this at
design time, but how can this be done in code at runtime?

TIA!

++++
Bill Sorensen
tzimi...@sprynet.com

 

Re:Calculated fields - how to add at runtime?


Quote
>>I want to add a calculated field (Deleted status, via an Advantage method) to the table when it's opened.

See: TI3171:Dynamically creating a TTable & fields at runtime

 http://www.inprise.com/devsupport/delphi/ti_list/TI3171.html

=Bill=

Re:Calculated fields - how to add at runtime?


    with FieldDefs do  
    begin      
    { Create a calculated TField, assign properties,
      and add to MyTable's field definitions array. }
      TaxAmount := TFloatField.Create(MyTable);      
      with TaxAmount do
      begin      
        FieldName := 'TaxAmount';      
        Calculated := True;
        Currency := True;        DataSet := MyTable;
        Name := MyTable.Name + FieldName;
        MyTable.FieldDefs.Add(Name, ftFloat, 0, false);    
      end;    
     end;

From the Online Help for TFieldDefs

Quote
>>Otherwise, FieldDefs is primarily intended for internal use by the other properties, events,
>>and methods of TDataSet and its descendants.

The key word there is "primarily".   <s>

This is the only way that I know of to add calculated fields at runtime.  
I have used this method successfuly.  

=Bill=

Re:Calculated fields - how to add at runtime?


On Sun, 22 Nov 98 17:58:36 PST, "WmBurke(TeamB)"

Quote
<no_spam_wm25bu...@earthlink.net> wrote:
>>>I want to add a calculated field (Deleted status, via an Advantage method) to the table when it's opened.

>See: TI3171:Dynamically creating a TTable & fields at runtime

> http://www.inprise.com/devsupport/delphi/ti_list/TI3171.html

>=Bill=

Thanks!  (I really should have checked the TI's first... sorry!)

However, I'm afraid to say this didn't answer my question.  It shows
how to create a calculated field on a new table created with
CreateTable.

The Delphi 3 help file, however, states that TDataSet.FieldDefs should
not be changed unless creating a new table with CreateTable.  I want
to programmatically add (i.e., in code, not via the Fields Editor) a
calculated field to an EXISTING table that already contains data.  I
don't want to copy the table to a new one, as the user may wish to
edit it.  Can this be done?

Re:Calculated fields - how to add at runtime?


On Sun, 22 Nov 98 20:15:37 PST, "WmBurke(TeamB)"

Quote
<no_spam_wm25bu...@earthlink.net> wrote:
>    with FieldDefs do  
>    begin      
>    { Create a calculated TField, assign properties,
>      and add to MyTable's field definitions array. }
>      TaxAmount := TFloatField.Create(MyTable);      
>      with TaxAmount do
>      begin      
>        FieldName := 'TaxAmount';      
>        Calculated := True;
>        Currency := True;        DataSet := MyTable;
>        Name := MyTable.Name + FieldName;
>        MyTable.FieldDefs.Add(Name, ftFloat, 0, false);    
>      end;    
>     end;

>From the Online Help for TFieldDefs
>>>Otherwise, FieldDefs is primarily intended for internal use by the other properties, events,
>>>and methods of TDataSet and its descendants.

>The key word there is "primarily".   <s>

>This is the only way that I know of to add calculated fields at runtime.  
>I have used this method successfuly.  

It works, but there's one not-so-small problem:  FieldDefs.Add raises
an exception if the table is active.  If I call it before I set Active
to True, it works and the calculated field gets instantiated, but none
of the table's actual fields appear.  If I call it after I set Active
to True, an exception is raised and it fails.

As the TTable object is "generic" (i.e., I don't know what table the
user will choose to open), I can't assume anything about the other
fields.

Any suggestions?

Re:Calculated fields - how to add at runtime?


Quote
>>  If I call it before I set Active
>>to True, it works and the calculated field gets instantiated, but none
>>of the table's actual fields appear.

You'll need to add the physical fields that you want displayed to FieldDefs - in addition
to the calculated fields.

=Bill=

Other Threads