Microsoft Access BeforeUpdate and AfterUpdate Events
Many times when working with data you’ll need to write some code or start a Macro that validates or changes the information being passed around in your database. When working on a form, you’ve got so many options available that it’s difficult to decide when to change data, when to check it, and where to go about doing all of this.
There are two events available to you when working with Controls and Forms: BeforeUpdate and AfterUpdate. The BeforeUpdate event will occur before a new value update, but after you’ve initiated the change (otherwise BeforeUpdate would just run constantly – something has to set it in motion). AfterUpdate is the event that takes place after the data has been updated or changed. But when do you use these events, and does it really matter where your code or macro goes?
When Using Controls
If you need to verify what data is being input into a control, then you should use the BeforeUpdate event. For example, when someone changes a field, you’ll need to check that field to make sure the data should be accepted and placed into the table. You wouldn’t do this on the AfterUpdate event, as it would be too late – the data would already be in the table. You can also compare the value that is going to be saved to the value that was previously in the control if it had a prior value. If, however, you need to change a value on your form depending on what was input into a certain field, you would use the AfterUpdate event to make that change as after the control is updated, you’ll be using the new value.
When Using Forms
Use the BeforeUpdate event for data validation. For instance if you wanted a custom message box to appear when someone tried to save a record, but entered incorrect data, or left data blank, you would set that Macro or Code on the BeforeUpdate event as the record won’t accept all of your changes, then check to see if they were correct. Any kind of change to the data – validation, calculations, or adding data – is best done on the BeforeUpdate event as you can better trap problems and avoid incorrect data being stored in underlying tables.
Use the AfterUpdate event on forms whenever you want to analyze the record that was just created either through looping through the recordset to analyze the most current information, or simply work a calculation or two.
The best advice anyone can give regarding these two events is to test extensively against sample data. By manipulating items right before a record saves or by saving data without testing it out first the possibility that procedure is wrong is greatly increased. Your best bet when building this type of code is to take it step by step. Don’t build an entire procedure only to realize that you’ve gotten the event wrong (in any case, not just BeforeUpdate and AfterUpdate), or go about it in a way that could be damaging to your data.
Test, test, test, and then test some more!
©2015 Blue Moose Technology, LLC
Article Publishing and Re-Print Guidelines
- The article text, resource box, URLs copyright information and these guidelines must be left intact and unchanged.
- When re-published online, all links must be live hyperlinks.
- These articles may not be distributed in any manner that does not comply with federal communication guidelines. Be Good!
- These articles must not be password protected or limited to membership or paid-only viewing. Keep 'em free, folks!
You can easily link to this article!
If you'd like to link to this article from your own website, copy and paste the following HTML code onto your page:
On your page, the code will appear like this: