Microsoft Access VBA events have been around forever! Well, not forever but since VBA has been (circa 20+ years) and it’s still widely unexplored beyond the basic event triggers one gets to see and know about namely the clicking of a Command Button on a form or when the form itself opens.
Many users and developers have used events in many ways which is not just exclusive to MS Access (Excel is also a very popular application) and to get a feel for how one could use an event, check out this six minute video tutorial which runs through the After Update event to calculate a total from two other field controls in a form…
The video demonstrates how you can create VBA code and attach it to predefined event signatures that each control and object hosts. One could argue however why would you bother using this example in the video when a calculated expression would support this action too? The quick answer could be you may want to further enhance the total or validate the total so the user could interact with it or it simply is a real field (which the example implies) and want the system to automatically store the updated value for you.
Access VBA Events: What Are Events And How Do We Use MS Access VBA Events
Here’s my explanation of an event...
This is where the Microsoft Access system listens for an event to take place. In fact, Access is always listening for events; we just don’t notice them as there’s usually no Access VBA code attached to that event.
However, when you write code (or use macros) and connect the procedure to an event then we take notice as the system comes to life.
There are many events in Access and the most common one is the On Click event normally attached to a Command Button control on a form which means when a user clicks on the button, it triggers the event and runs the procedure.
Another example of an event is for a Combo Box control called After Update which means when a user has selected an item in the drop-down box the event is triggered here too.
You don’t actually create the opening and closing signatures to events as you would when creating your own sub procedures – it’s generated for you when navigating via a control or object in the Property Sheet pane’s Event tab.
In the illustration above, you can see a Combo Box called cbo_User which has an event known as After Update that generated the signature cbo_User_AfterUdate().
When the user selects an item in this Combo Box, it triggers the VBA code which runs one line at time in the order instructed (it appears). This examples prevents the user having to manually empty the contents of a field (if not already clear), moving the insertion to the next control ready for a password and then switches on another control if the user has authorisation to use that control – all which smooth’s and speeds up the user’s interaction.
Working with Access VBA events will certainly be more natural for end-users who use your database but have no appreciation or wish to know or understand their benefits and why they are important.
Also worth a mention is that developers sometimes get confused with the order of how events (where applicable) are triggered and that they may appear or do the same thing. For example, when a form is opened or closed there are three events for each occasion:
Opening… runs the Open, Load and then Activate events in that order.
Closing… runs the Deactivate, Unload and then Close events in that order.
Checkout this article from Microsoft on the order of events for database objects for more background information.
You may also want to know more about that illustration above and how events work with a couple of real world utilities:
Login User Form Utility offer with user guide and finished product or the extended version of Login & Workgroup With Access Rights utility offer again with a full user guide and free bonus offers.
Why not tell me your favourite or most popular event you use in the comments box below.