MS-Access / Getting Started

Understanding how events trigger VBA code

You can create an event procedure that runs when a user performs any one of the many different events that Access recognizes. Access responds to events through special form and control properties. Reports have a similar set of events, tailored to the special needs and requirements of reports.

Creating event procedures

In Access, you execute event procedures through an object's event properties.

Access provides event properties you use to tie VBA code to an object's events. For example, the On Open property is associated with a form or report opening on the screen.

Note: Access event procedures, as seen in the Property Sheet, often contain spaces. For instance, the Open event appears as the On Open event procedure. The event itself, of course, is Open. Many, but not all, event property names begin with On.

You add an event procedure to a form or report by selecting the event property in the object's Property Sheet. If no event procedure currently exists for the property, a drop-down arrow and builder button appear in the property's box.

The drop-down button exposes a short list that contains [Event Property]. Selecting this option and then clicking on the builder button, takes you to the VBA code editor with an event procedure template already in place.

Notice the general format of the event procedure's declaration:

Private Sub Object_Event()

The Object portion of the procedure's name is, of course, the name of the object raising the event, while the Event portion is the specific event raised by the object. The object is Form and the event is BeforeUpdate. Some events support arguments, which appear within the parentheses at the end of the declaration.

In case you're wondering, you can't change the name, or the arguments, of an event procedure and expect it to continue working. Access VBA relies on the Object_Event naming convention to tie a procedure to an object's event.

[Previous] [Contents] [Next]