Calculations on Record Change

gmatriix

Registered User.
Local time
Today, 03:30
Joined
Mar 19, 2007
Messages
365
Hello All,

I have a form and some calculations in vba I would like to perform when any record on the form is changed.

I know I can do this on the control level but I want to just have one code that does all the calculation on the form level. I cannot figure out which event will trigger the calculations immediately after the record is changed.

I can get it to do it but I have to move the record first and then it will show results.

Is there anyway to perform these calculations immediately when a record on the form has been changed?

Any Ideas?

Thanks
 
The Form Dirty would be triggered as soon as some change is made to the data on the Form..
 
Thanks.....what if the record that you are changing is need to perform the calculation? I believe on dirty will not wait until you enter the value then calculate will it? or am I wrong.....
 
I believe on dirty will not wait until you enter the value then calculate will it? or am I wrong.....
No, you are not wrong.. I picked up on your original premise..
I have a form and some calculations in vba I would like to perform when any record on the form is changed.
:
Is there anyway to perform these calculations immediately when a record on the form has been changed?
However your second post...
what if the record that you are changing is need to perform the calculation?
suggests something different.. This would IMO go into the Control's BeforeUpdate event..
 
So do I have to put calculation on each control that will calculate if it is changed? I guess I was trying to find a way to do this with one code.

Or could I have one fuction and have each control refer to the function?

Thanks
 
Yes, you can create one function and all the BeforeUpdate events can call that one function..
 
Use the FORM's BeforeUpdate event. It runs once only and is the LAST event to run before the record is actually saved.
 
i agree with Pat #8 in general terms, although it all depends on the timing you actually need.

if a record has changed in any way, when it gets saved, the beforeupdate event and afterupdate event happens, and therefore you can do general housekeeping there. If you want to change the record BEFORE the save or validate the changes and potentially cancel the update, then beforeupdate. If it is something that occurs as a result of the change then maybe afterupdate is better.

HOWEVER - if you want to change things DURING the record edit, then you need to do it a different point. The problem with using the dirty event is you only get it once. THe first change makes a record dirty, and other changes don;t make it dirty again?

the safest way might be to put code on every control box - although as you say, it can be a lto of work.
 
Dave, the Form's BeforeUpdate and AfterUpdate are not interchangeable. The BeforeUpdate runs BEFORE the record is saved and the AfterUpdate runs AFTER the record is saved. You can't cancel the save in the AfterUpdate, it is too late. The record is already saved. Plus, if you dirty the record in the Form's AfterUpdate event, you put Access into a loop. Earlier versions of Access would actually lock up but newer versions seem to recognize the situation when the stack gets too deep and works itself out without freezing.

In general, if the edit or calculation involves a SINGLE field and nulls are allowed, control level events can be used and will give the user more immediate feedback. However, if you need to compare two fields such as to validate a date range or prevent some field from being null if another field has a value, you really must use the Form's BeforeUpdate event. If you try to put the code into individual control events, you will need it in several places and you will still miss nulls if the user never actuall tabs into the control. The BeforeUpdate event is much cleaner and neater.

As to the Dirty event, you would never use it for something like this. As Dave mentioned, it only runs ONCE for the entire form and it runs when the first character is typed so you simply can't use it as a place for validation code.

I would use it to check credentials. If some users were allowed to update and others were not, I would verify that in the Dirty event. This keeps you from having to run code in the current event to lock a form for some users/conditions and unlock it for others. I know the point is subtle but having worked in environments that had to process thousands of transactions a second, I am quite sensitive to efficiencies like this. Since most people will not attempt to edit data if they know they are not supposed to, you don't need to run the code to lock everything for every record. All you need to do is to intercept the change attempt and stop it. So instead of running the code a thousand times, you run it once.

"Immediately" is the trigger word in the question. What does that mean? Is it as soon as a single character is typed? (Dirty) Is it as soon as a single field is entered? (control's BeforeUpdate), Is it as soon as all the necessary fields are entered? (form's BeforeUpdate)
 
Last edited:

Users who are viewing this thread

Back
Top Bottom