Order of events for database objects

Microsoft Office Access 2003

Events occur for controls on forms when you move the focus to a control, and when you change and update data in a control.

ShowMoving the focus to a control

When you move the focus to a control on a form— for example, by opening a form that has one or more active controls or by moving to another control on the same form— the Enter and GotFocus events occur in this order:

EnterGotFocus

If you are opening a form, these events occur after the events associated with opening the form (such as Open, Activate, and Current), as follows:

Open (form) → Activate (form) → Current (form) → Enter (control) → GotFocus (control)

When the focus leaves a control on a form— for example, when you close a form that has one or more active controls or move to another control on the same form— the Exit and LostFocus events occur in this order:

ExitLostFocus

If you are closing a form, the Exit and LostFocus events occur before the events associated with closing the form (such as Unload, Deactivate, and Close), as follows:

Exit (control) → LostFocus (control) → Unload (form) → Deactivate (form) → Close (form)

ShowChanging and updating data in a control

When you enter or change data in a control on a form and then move the focus to another control, the BeforeUpdate and AfterUpdate events occur:

BeforeUpdateAfterUpdate

The Exit and LostFocus events for the changed control occur after the BeforeUpdate and AfterUpdate events:

BeforeUpdateAfterUpdateExitLostFocus

When you change the text in a text box or in the text box portion of a combo box, the Change event occurs. This event occurs whenever the contents of the control change, but before you move to a different control or record (and thus, before the BeforeUpdate and AfterUpdate events occur). The following sequence of events occurs for each key you press in a text box or in the text box portion of a combo box:

KeyDownKeyPressDirtyChangeKeyUp

The NotInList event occurs after you enter a value in a combo box that isn't in the combo box list and then attempt to move to another control or record. It occurs after the keyboard events and the Change events for the combo box, but before any other control or form events. If the LimitToList property of the combo box is set to Yes, the Error event for the form occurs immediately after the NotInList event:

KeyDownKeyPressDirtyChangeKeyUpNotInListError

ShowOrder of events for records on forms

Events occur for records on forms when you move the focus to a record, update data in a record, delete an existing record or records, or create a new record.

ShowMoving the focus to records and updating data in records

When you move the focus to an existing record on a form, enter or change data in the record, and then move the focus to another record, the following sequence of events occurs for the form:

Current (form) → BeforeUpdate (form) → AfterUpdate (form) → Current (form)

When you leave the record whose data has changed, but before you enter the next record, the Exit and LostFocus events occur for the control with the focus. These events occur after the BeforeUpdate and AfterUpdate events for the form, as follows:

BeforeUpdate (form) → AfterUpdate (form) → Exit (control) → LostFocus (control) → RecordExit (form) → Current (form)

As you move the focus among the controls on a form, events occur for each control. For example, the following sequences of events occur when you:

  • Open a form and change data in a control:

    Current (form) → Enter (control) → GotFocus (control) → BeforeUpdate (control) → AfterUpdate (control)

  • Move the focus to another control:

    Exit (control1) → LostFocus (control1) → Enter (control2) → GotFocus (control2)

  • Move the focus to another record:

    BeforeUpdate (form) → AfterUpdate (form) → Exit (control2) → LostFocus (control2) → RecordExit (form) → Current (form)

ShowDeleting records

When you delete a record, the following events occur for the form, and Microsoft Access displays a dialog box asking you to confirm the deletion:

DeleteBeforeDelConfirmAfterDelConfirm

If you cancel the Delete event, the BeforeDelConfirm and AfterDelConfirm events don't occur and the dialog box isn't displayed.

ShowCreating a new record

When you move the focus to a new (blank) record on a form and then create a new record by typing in a control, the following sequence of events occurs:

Current (form) → Enter (control) → GotFocus (control) → BeforeInsert (form) → AfterInsert (form)

The BeforeUpdate and AfterUpdate events for the controls on the form and for the new record occur after the BeforeInsert event and before the AfterInsert event.

ShowOrder of events for forms and subforms

Events occur for forms when you open or close a form, move between forms, or work with data on a form.

ShowOpening and closing a form

When you open a form, the following sequence of events occurs for the form:

OpenLoadResizeActivateCurrent

If there are no active controls on the form, the GotFocus event also occurs for the form after the Activate event but before the Current event.

When you close a form, the following sequence of events occurs for the form:

UnloadDeactivateClose

If there are no active controls on the form, the LostFocus event also occurs for the form after the Unload event but before the Deactivate event.

ShowMoving between forms

When you switch between two open forms, the Deactivate event occurs for the first form, and the Activate event occurs for the second form:

Deactivate (form1) → Activate (form2)

The Deactivate event for a form also occurs when you switch from the form to another window in Microsoft Access. However, the Deactivate event doesn't occur when you switch to a dialog box, to a form whose PopUp property is set to Yes, or to a window in another application.

Note   An Open event doesn't occur if you move to a form that is already open, even if you've moved to the form by carrying out an OpenForm action.

ShowWorking with data on a form

Form and control events occur as you move between records in the form and change data. For example, when you first open a form, the following sequence of events occurs:

Open (form) → Load (form) → Resize (form) → Activate (form) → Current (form) → Enter (control) → GotFocus (control)

Similarly, when you close a form, the following sequence of events occurs:

Exit (control) → LostFocus (control) → Unload (form) → Deactivate (form) → Close (form)

If you've changed data in a control, the BeforeUpdate and AfterUpdate events for both the control and the form occur before the Exit event for the control.

ShowWorking with subforms

When you open a form containing a subform, the subform and its records are loaded before the main form. Thus, the events for the subform and its controls (such as Open, Current, Enter, and GotFocus) occur before the events for the form. The Activate event doesn't occur for subforms, however, so opening a main form triggers an Activate event only for the main form.

Similarly, when you close a form containing a subform, the subform and its records are unloaded after the form. The Deactivate event doesn't occur for subforms, so closing a main form triggers a Deactivate event only for the main form. The events for the controls, form, and subform occur in the following order:

  1. Events for the subform's controls (such as Exit and LostFocus)
  2. Events for the form's controls (including the subform control)
  3. Events for the form (such as Deactivate and Close )
  4. Events for the subform

Note   Because the events for a subform occur after the main form is closed, certain events such as canceling the closing of the main form from an event in the subform, will not occur. You may need to move these types of validations to an event on the main form.

ShowOrder of events for keystrokes and mouse clicks

Keyboard events occur for forms and controls when you press keys or send keystrokes while the form or control has the focus. Mouse events occur for forms, form sections, and controls on forms when you press the mouse buttons while the mouse pointer is on a form, section, or control, or when you move the mouse pointer over a form, section, or control.

ShowKeyboard events

When you press and release a key while a control on a form has the focus (or use the SendKeys action or statement to send a keystroke), the following sequence of events occurs:

KeyDownKeyPressKeyUp

When you press and release a key or send a keystroke in the ANSI character set key, the KeyDown, KeyPress, and KeyUp events all occur. If you press and hold down an ANSI key, the KeyDown and KeyPress events alternate repeatedly (KeyDown, KeyPress, KeyDown, KeyPress, and so on) until you release the key; then the KeyUp event occurs.

If you press and release a non-ANSI key, the KeyDown and KeyUp events occur. If you press and hold down a non-ANSI key, the KeyDown event occurs repeatedly until you release the key; and then the KeyUp event occurs.

If pressing a key triggers another event for a control, the other event occurs after the KeyPress event but before the KeyUp event. For example, if a keystroke changes text in a text box, triggering a Change event, the following sequence of events occurs:

KeyDownKeyPressChangeKeyUp

If a keystroke causes the focus to move from one control to another control, the KeyDown event occurs for the first control, while the KeyPress and KeyUp events occur for the second control. For example, if you change data in a control and then press the TAB key to move to the next control, the following sequences of events occur:

  • First control:

    KeyDownBeforeUpdateAfterUpdateExitLostFocus

  • Second control:

    EnterGotFocusKeyPressKeyUp

ShowMouse events

When you press and release a mouse button while the mouse pointer is on a control on a form, the following sequence of events occurs for the control:

MouseDownMouseUpClick

If a control has the focus and you click another control to move the focus to this second control, the following sequences of events occur:

  • First control:

    ExitLostFocus

  • Second control:

    EnterGotFocusMouseDownMouseUpClick

If you move to another record and then click a control, the Current event for the form also occurs before the Enter event for the control.

Double-clicking a control causes both DblClick and Click events to occur. For example, when you double-click a control other than a command button, the following sequence of events occurs for the control:

MouseDownMouseUpClickDblClickMouseUp

When you double-click a command button, these events occur, followed by a second Click event.

The MouseMove event for a form, section, or control occurs when you move the mouse pointer over the form, section, or control. This event is independent of the other mouse events.

ShowOrder of events for reports and report sections

Events occur for reports and report sections when you open a report to print or preview it, or close a report.

ShowEvents for reports

When you open a report to print or preview it and then later close the report or move to another window in Microsoft Access, the following sequence of events occurs for the report:

OpenActivateCloseDeactivate

When you switch between two open reports, the Deactivate event occurs for the first report, and the Activate event occurs for the second report:

Deactivate (report1) → Activate (report2)

The Deactivate event for a report also occurs when you switch from the report to another window in Access. However, the Deactivate event doesn't occur when you switch to a dialog box, to a form whose PopUp property is set to Yes, or to a window in another application.

When you open a report that is based on a query, Access triggers the Open event before running the underlying query. As a result, you can set the criteria for the report by using a macro or event procedure that responds to the Open event. For example, the macro or event procedure could open a custom dialog box in which you enter the report criteria.

ShowEvents for report sections

When you print or preview a report, the Format and Print events occur for the report sections after the Open and Activate events for the report and before the report's Close or Deactivate events:

Open (report) → Activate (report) → Format (report section) → Print (report section) → Close (report) → Deactivate (report)

In addition, the following events can occur during or after formatting but before the Print event:

  • The Retreat event occurs when Access returns to a previous section during report formatting.
  • The NoData event occurs if there are no records displayed by the report.
  • The Page event occurs after formatting but before printing. You can use this event to customize the appearance of the printed report.