Dophia and
egims
The third rule of normalization is not to store calculated values.
or
The reason is that calculated values can always be re-calculated, and more importantly, they can become a "maintenance" item.
However, there are times when you use some common sense. For example, you have a General Ledger system where you want to see monthly balances, and the monthly balances are based on 10,000x of transactions. A) after closing / finalizing a month, it is highly unlikely monthly transactions need to be recaluclated; B) Some time down the road, you may need to free up space and deleted old transaction.
It makes sense to store monthly transactions because a) the numbers will not change; b) you can / may need to get rid of the detail records but still maintain some of the historic picture.
...Moving on
Instead of the using the ControlSource to depict the calculated fields...[tt]
=[Your1stField] + [Your2ndField] + [Your3rdField]...[/tt]
You use "Event Procedures".
Assumptions
- CalcValue is the name of the cacluated field
- Your1stField, Your2ndField... name of data fields
For "AfterUpdate" event for each Your???Field, you use some simple coding...
[tt]
Me.CalcValue = Nz(Me.Your1stField, 0) + Nz(Your2ndField, 0) ....[/tt]
The Nz() function accommodates null / empty values where it substitutes 0 for the null value - otherwise, you would get an error.
You can also use the same formula for the BeforeUpdate record event procedure.
BTW, since you re-caculate the value several times on the form - AfterUpdate field event for each appropriate field, and for the BeforeUpdate record event, you can use a centralized approach by calling a central routine...
Code:
Private Sub ReCalcMySum()
Me.CalcValue = Nz(Me.Your1stField, 0) + Nz(Your2ndField, 0)
End Sub
Then for each of the appropriate event procedures,
...Like I said, real simple.
...Moving on
I suspect you will need a bit of info in how to create an Event Procedure.
Have the form open in design mode. Make sure the "Properties" window is open ("From the menu, "View" -> "Properties")
Select one of the fields that the calculated value will be based on. The select the "Event" tab on the Properties window, then select the field, "AfterUpdate". Pick [Event Procedure] from the pick list. You will see a command button to the right with "..." - click the command button. This will open up the coding window, and generate a bit of code...
Code:
Private Sub Your1stField_AfterUpdate()
End Sub
Enter the
ReCalcMySum in between the Private Sub... and ...End Sub
Then after the "End Sub" line, enter
Private Sub MyReCalcSum() and hit the <Enter> key. Type in your calculation formula.
You call this subroutine anywhere from within the Form.
On last thing. To create an event procedure for the entire form, select the "square" in the top left corner where the vertical ruler meets the horizontal ruler.
Richard