Open, Close Events

Applies To

Form, Report.

Description

These events occur:

  • Open — for forms, the Open event occurs when a form is opened, but before the first record is displayed. For reports, the event occurs before a report is previewed or printed.
  • Close — occurs when a form or report is closed and removed from the screen.

Remarks

By running a macro or an event procedure when a form’s Open event occurs, you can close another window or move the focus to a particular control on a form. You can also run a macro or an event procedure that asks for information needed before the form or report is opened or printed. For example, an Open macro or event procedure can open a custom dialog box in which the user enters the criteria for the set of records to display on a form or the date range to include for a report.

The Open event doesn’t occur when you open a form that’s already open — for example, when you switch to the form from another window in Microsoft Access or use the OpenForm action in a macro to bring the open form to the top.

When you open a form, Microsoft Access runs the underlying query for the form before it runs the Open macro or event procedure. However, when you open a report, Microsoft Access runs the Open macro or event procedure before it runs the underlying query for the report. This enables the user to specify criteria for the report before it opens — for example, in a custom dialog box you display when the Open event occurs.

If your application can have more than one form loaded at a time, use the Activate and Deactivate events instead of the Open event to display and hide custom toolbars when the focus moves to a different form.

The Open event occurs before the Load event, which is triggered when a form is opened and its records are displayed.

When you first open a form, the following events occur in this order:

Open Load Resize Activate Current

The Close event occurs after the Unload event, which is triggered after the form is closed but before it is removed from the screen.

When you close a form, the following events occur in this order:

Unload Deactivate Close

When you close a report, the following events occur in this order:

Unload Close Deactivate

When the Close event occurs, you can open another window or request the user’s name to make a log entry indicating who used the form or report.

If you’re trying to decide whether to use the Open or Load event for your macro or event procedure, one significant difference is that the Open event can be canceled, but the Load event can’t. For example, if you’re dynamically building a record source for a form in an event procedure for the form’s Open event, you can cancel opening the form if there are no records to display. Similarly, the Unload event can be canceled, but the Close event can’t.

See Also

Activate, Deactivate Events; GotFocus, LostFocus Events; GoToRecord Action; Load, UnLoad Events; Resize Event; SetFocus Method.