Part II: Programming Microsoft Access
454
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 prop-
erty names begin with On.
You add an event procedure to a form or report by selecting the event property (Before Update,
for this example) in the object’s Property Sheet. If no event procedure currently exists for the prop-
erty, a drop-down arrow and builder button appear in the property’s box, as shown in the Before
Update event property in Figure 12.1.
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 (see Figure 12.2).
FIGURE 12.2
An empty event procedure template for the form’s BeforeUpdate event.
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. In Figure 12.2, 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.
Identifying Common Events
Certain events are raised by many different Access objects. Microsoft has taken great care that these
events behave exactly the same way, regardless of the object raising them. Table 12.1 lists several