Board index » delphi » Help! Adding calculated fields at runtime!

Help! Adding calculated fields at runtime!

Has anyone managed to add a calculated field to a table at runtime?
I have a boolean variable 'IsComplete' in my form class, and I do the
following:

with Table1 do
begin
   IsComplete := True;
   OnCalcFields := Table1CalcFields;
   FieldDefs.Update;
   FieldDefs.Add('Complete', ftBoolean, 0, True);

   FieldByName('Complete').Calculated := True;
   { ^^^ EXCEPTION GENERATED HERE  ^^^ }
end;

.
.
.

procedure TForm1.Table1CalcFields(Sender: TDataSet)
begin
   Sender.FieldByName('Complete').AsBoolean := IsComplete;
end;

So later on I want to be able to define what the current 'Complete' field is
by setting IsComplete as needed. Does this seem right? As it is, it generates
an exception on the marked line, saying that field 'Complete' is not found. I
have traced through, and Delphi says the following:

Table1.FieldDefs.Count = 20;
Table1.FieldDefs[19].Name = 'Complete';
Table1.FieldCount = 19; ?????

So 'Complete' is in the field definitions but not in the table? If it matters,
all my tables are Paradox.
Help!! Thanx in advance... !

-|\----/|---/~|--|~|---------------------------------------------
 | \  / |  /  |  | |    "Welcome to heaven. Here's your harp."
 |  \/  | / / |__| |__  "Welcome to hell. Here's your accordion."
 |      |/ /| |__| | /                                --Larsen
 | |\/|   /\  |  |  /   Attempting world's chunkiest .sig record:
 |_|  |__/  \_|  |_/    The Mabster <====> mhamil...@bunge.com.au

 

Re:Help! Adding calculated fields at runtime!


Quote
mhamil...@bunge.com.au (Mab) wrote:
> Has anyone managed to add a calculated field to a table at runtime?
> I have a boolean variable 'IsComplete' in my form class, and I do the
> following:

> with Table1 do
> begin
>    IsComplete := True;
>    OnCalcFields := Table1CalcFields;
>    FieldDefs.Update;
>    FieldDefs.Add('Complete', ftBoolean, 0, True);

>    FieldByName('Complete').Calculated := True;
>    { ^^^ EXCEPTION GENERATED HERE  ^^^ }
> end;

> .
> .
> .

> procedure TForm1.Table1CalcFields(Sender: TDataSet)
> begin
>    Sender.FieldByName('Complete').AsBoolean := IsComplete;
> end;

> So later on I want to be able to define what the current 'Complete' field is
> by setting IsComplete as needed. Does this seem right? As it is, it generates
> an exception on the marked line, saying that field 'Complete' is not found. I
> have traced through, and Delphi says the following:

> Table1.FieldDefs.Count = 20;
> Table1.FieldDefs[19].Name = 'Complete';
> Table1.FieldCount = 19; ?????

As far as I understand, and I don't much in this case, FieldDefs holds
a FieldDef for every single _physical_ column in the table. So, while
adding to fielddefs suggests that you are modifying your table's
structure, it is not - lucky us...:-) - which explains why
fielddefs.count returns a number different from fieldcount. Fieldcount
returns the number of columns in the dataset. If you had limited that
to only a few columns, that's the count you would have seen...

What you - and I - were missing is the fact that there are in fact no
TFields added to the dataset and that's the bottleneck causing the
exception. Anyway, I do it like follows (forgive me the standard
Delphi syntax on that calculated field) and it works:

   Table1Complete := TBooleanField.Create(Table1);
   Table1Complete.FieldName := 'Complete';
   Table1Complete.Calculated := True;
   Table1Complete.DisplayLabel := 'Complete';
   Table1.DisableControls;
   Table1.Close;
   Table1Complete.Dataset := Table1;
   Table1.Open;
   Table1.EnableControls;

Of course, Table1Complete is a TBooleanField of the TForm1 class.

Have fun!

Jasper

PS: please take into consideration
- when replying, I just think I know;
- when asking, be sure that I don't.

Other Threads