Event Sequence
Sometimes even a fairly simple action on the part of the user raises multiple events in rapid succession. 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-3).
Looking at common event sequences
Here are the sequences of events for the most frequently encountered form scenarios:
- Opening and closing forms
- When a form opens: Open (form) → Load (form) → Resize (form) → Activate (form) → Current (form) → Enter (control) → GotFocus (control)
- When a form closes: Exit (control) → LostFocus (control) → Unload (form) → Deactivate (form) → Close (form)
- Changes in focus
- When the focus moves from one form to another: Deactivate (form1) → Activate (form2)
- When the focus moves to a control on a form: Enter → GotFocus
- When the focus leaves a form control: Exit → LostFocus
- When the focus moves from control1 to control2: Exit (control1) → LostFocus (control1) → Enter (control2) → GotFocus (control2)
- 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)
- When the focus moves to an existing record in Form view: BeforeUpdate (form) → AfterUpdate (form) → Current (form)
- Changes to data
- When data is entered or changed in a form control and the focus is moved to another control: BeforeUpdate → AfterUpdate → Exit → LostFocus
- When the user presses and releases a key while a form control has the focus: KeyDown → KeyPress → KeyUp
- When text changes in a text box or in the text-box portion of a combo box: KeyDown → KeyPress → Change → KeyUp
- When a value that is not present in the drop-down list is entered into a combo box's text area: KeyDown → KeyPress → Change → KeyUp → NotInList → Error
- When data in a control is changed and the user presses Tab to move to the next control:
Control1: KeyDown → BeforeUpdate → AfterUpdate → Exit → LostFocus
Control2: Enter → GotFocus → KeyPress → KeyUp - When a form opens and data in a control changes: Current (form) → Enter (control) → GotFocus (control) → BeforeUpdate (control) → AfterUpdate (control)
- When a record is deleted: Delete → BeforeDelConfirm → AfterDelConfirm
- When the focus moves to a new blank record on a form and a new record is created when the user types in a control: Current (form) → Enter (control) → GotFocus (control) → BeforeInsert (form) → AfterInsert (form)
- Mouse events
- When the user presses and releases (clicks) a mouse button while the mouse pointer is on a form control: MouseDown → MouseUp → Click
- When the user moves the focus from one control to another by clicking the second control:
Control1: Exit → LostFocus
Control2: Enter → GotFocus → MouseDown → MouseUp → Click - When the user double-clicks a control other than a command button: MouseDown → MouseUp → Click → DblClick → MouseUp