Board index » delphi » Calkulated fields

Calkulated fields

I have two calkulated fields in a tabel; Sum and Totalsum.
I want both of them to be automaticly updated, so I have them under
OnCalcFields, like this:

table4.fieldbyname('Sum').value:=table4.fieldbyname('Antall').value*table4.f
ieldbyname('Enhetspris').value;
table4.fieldbyname('Totalsum').value:=table4.fieldbyname('Totalsum').value+t
able4.fieldbyname('Sum').value;

But it is only the first (Sum) calkulated field that is beeing updated.
Nothing happens to the second field (Totalsum).

Is there a reason for this? I dont get any errormessages. (except that the
last calkulated field is still blank.)

Regards,
Roald

 

Re:Calkulated fields


On 16 Feb 1999 12:15:44 GMT, "Roald Simonsen"

Quote
<roald.simon...@kvaerner.com> wrote:
>I have two calkulated fields in a tabel; Sum and Totalsum.
>I want both of them to be automaticly updated, so I have them under
>OnCalcFields, like this:

>table4.fieldbyname('Sum').value:=table4.fieldbyname('Antall').value*table4.f
>ieldbyname('Enhetspris').value;
>table4.fieldbyname('Totalsum').value:=table4.fieldbyname('Totalsum').value+t
>able4.fieldbyname('Sum').value;

>But it is only the first (Sum) calkulated field that is beeing updated.
>Nothing happens to the second field (Totalsum).

You are probably not initializing totalsum correctly.  Beware though
that what you are doing will not work properly.  You have absolutely
no control on when and on how many times the OnCalcFields event will
be called.  You cannot use it to calculate sums (or other info) across
multiple records.  To do a sum of all records, you either:
 * cycle through the dataset, beforehand, and calculate it by code
 * use a sum query to have it calculated by the engine or BDE

The second will not work in your case because you want to sum a field
which is calculated by the app and does not exist in the dataset.  You
could have it in the dataset by using a query like:
   SELECT *,Antall*Enhetspris AS SUM FROM TABLE

Regards,

--
Marco Rocci
MicroEra srl
Turin, Italy

Other Threads