Board index » delphi » Calculated fields - writing value to Db

Calculated fields - writing value to Db

I'm using a calculated field on a TQuery in a DBGrid. I've coded
the OnCalcFields and the field on the grid is updated exactly as
expected. The field on the grid has a corresponding database
field behind it, but the database never gets updated with the
calculated values.

Are calculated fields effectively display only or should the Db fields
be updated (i.e. am I not doing something...)?

Any help gratefully received. Thanks.
--  
nev.

n...@rimes.demon.co.uk

 

Re:Calculated fields - writing value to Db


Quote
Nevil Rimes <n...@rimes.demon.co.uk> wrote:
>I'm using a calculated field on a TQuery in a DBGrid. I've coded
>the OnCalcFields and the field on the grid is updated exactly as
>expected. The field on the grid has a corresponding database
>field behind it, but the database never gets updated with the
>calculated values.
>Are calculated fields effectively display only or should the Db fields
>be updated (i.e. am I not doing something...)?

Calculated fields are only for display. They behave a bit special.
Note that you should not use the OnCalcFields event for anything else
than calculated fields on the very record you are working with.

If you want to save the result in the database, you have to do that
manually in a suitable event, as far as I can find out right now
(without currently running Delphi), the AfterPost event should work
fine.

Anders

Re:Calculated fields - writing value to Db


Quote
In article <4n310o$...@mn5.swip.net> m-29...@mailbox.swipnet.se (Anders Gustavsson) writes:
>>I'm using a calculated field on a TQuery in a DBGrid. I've coded
>>the OnCalcFields and the field on the grid is updated exactly as
>>expected. The field on the grid has a corresponding database
>>field behind it, but the database never gets updated with the
>>calculated values.
>>Are calculated fields effectively display only or should the Db fields
>>be updated (i.e. am I not doing something...)?
>Calculated fields are only for display. They behave a bit special.
>Note that you should not use the OnCalcFields event for anything else
>than calculated fields on the very record you are working with.
>If you want to save the result in the database, you have to do that
>manually in a suitable event, as far as I can find out right now
>(without currently running Delphi), the AfterPost event should work
>fine.

Gremlins again, Anders; surely you meant to say "BeforePost."

Calculated fields are computed by your program in response to an
"OnCalcFields" event; otherwise they look, smell, and taste exactly like the
other "real" fields.  But they don't get stored into the database.

Delphi blocks you from changing the value of any NON-calculated fields in an
OnCalcFields event, as a guard against endless recursion recursion recursion
recursion (oops!) but you can save them before-post.

/mr/

Re:Calculated fields - writing value to Db


Quote
Nevil Rimes <n...@rimes.demon.co.uk> wrote:
>I'm using a calculated field on a TQuery in a DBGrid. I've coded
>the OnCalcFields and the field on the grid is updated exactly as
>expected. The field on the grid has a corresponding database
>field behind it, but the database never gets updated with the
>calculated values.
>Are calculated fields effectively display only or should the Db fields
>be updated (i.e. am I not doing something...)?
>Any help gratefully received. Thanks.
>--  
>nev.
>n...@rimes.demon.co.uk

I had same problem.    Change your calcfield to a regular field so the
database cand be updated.  Then put your function or calucaltion in an
'onValidate' event refernecing any filed you are using.  Then if you
or your users ever change those fields the calculated value will also
be updated.

JDB

Other Threads