Save calculated values

  • Thread starter Thread starter Nick
  • Start date Start date
N

Nick

I have a calculated value in a form. Is it ever possible
to save the value into the source table?
 
Yes, but the question is should you save calculated data. Generally
speaking, the answer is no, although there can be times when doing so makes
sense. However, depending on how you calculte the value, you can either (a)
set the contolsource = the name of the field to save to or (b) run an Update
query in the one of the Forms events (like BeforeUpdate, BeforeInsert, etc)
that sets the value appropriately.
 
I have a calculated value in a form. Is it ever possible
to save the value into the source table?

Yes; but usually you don't want to do so.

Storing derived data such as this in your table accomplishes
three things: it wastes disk space; it wastes time (almost
any calculation will be MUCH faster than a disk fetch); and
most importantly, it risks data corruption. If one of the
underlying fields is subsequently edited, you will have data
in your table WHICH IS WRONG, and no automatic way to detect
that fact.

Just redo the calculation whenever you need it, either as a
calculated field in a Query or just as you're now doing it -
in the control source of a Form or a Report textbox.

If you have some good reason to store this data (for instance, if the
calculated value needs to be kept constant even if the underlying
values change), you can do so several ways; one would be to have a
second textbox on the form bound to the table field (perhaps with its
Visible property set to False), and use code in the Form's
BeforeUpdate event to set the value of the bound textbox to the value
in the calculated textbox.
 
Back
Top