Chapter 12: The Access Event Model
465
Paying Attention to Event Sequence
Sometimes even a fairly simple action on the part of the user raises multiple events in rapid succes-
sion. As an example, every time the user presses a key on the keyboard, the KeyDown, KeyPress,
and KeyUp events are raised. Similarly, pressing the left mouse button fires the MouseDown
and MouseUp events, as well as a Click event. It’s your prerogative as a VBA developer to decide
which events you program in your Access applications.
Events don’t occur randomly. Events actually fire in a predictable fashion, depending on which
control is raising the events. Sometimes the trickiest aspect of working with events is keeping track
of the order in which events occur. It may not be intuitive, for example, that the Enter event
occurs before the GotFocus event (see Table 12.2) or that the KeyDown event occurs before the
KeyPress event (see Table 12.3).
Looking at common event sequences
Here are the sequences of events for the most frequently encountered form scenarios:
l (^) Opening and closing forms
l When a form opens: Open (form) → Load (form) → Resize (form) → Activate
(form) → Current (form) → Enter (control) → GotFocus (control)
l When a form closes: Exit (control) → LostFocus (control) → Unload (form) →
Deactivate (form) → Close (form)
l Changes in focus
l (^) When the focus moves from one form to another: Deactivate (form1) →
Activate (form2)
l (^) When the focus moves to a control on a form: Enter → GotFocus
l When the focus leaves a form control: Exit → LostFocus
l (^) When the focus moves from control1 to control2: Exit (control1) → LostFocus
(control1) → Enter (control2) → GotFocus (control2)
l (^) When the focus leaves the record in which data has changed, but before entering the
next record: BeforeUpdate (form) → AfterUpdate (form) → Exit (control) →
LostFocus (control) → Current (form)
l When the focus moves to an existing record in Form view: BeforeUpdate (form) →
AfterUpdate (form) → Current (form)
l Changes to data
l (^) When data is entered or changed in a form control and the focus is moved to another
control: BeforeUpdate → AfterUpdate → Exit → LostFocus
l (^) When the user presses and releases a key while a form control has the focus:
KeyDown → KeyPress → KeyUp