Load, Unload Events
Applies To
Form.
Description
- The Load event occurs when a form is opened and its records are displayed.
- The Unload event occurs after a form is closed but before it's removed from the screen. When the form is reloaded, Microsoft Access redisplays the form and reinitializes the contents of all its controls.
Remarks
To run a macro or event procedure when these events occur, set the OnLoad, or OnUnload property to the name of the macro or to [Event Procedure].
The Load event is caused by user actions such as:
- Starting an application.
- Opening a form by clicking Open in the Database window.
- Running the OpenForm action in a macro.
The Unload event is caused by user actions such as:
- Clicking a Form window's Close button or clicking Close on the File menu or a form's Control menu.
- Running the Close action in a macro.
- Quitting an application by right-clicking the application's taskbar button and then clicking Close.
- Quitting Windows while an application is running.
By running a macro or an event procedure when a form's Load event occurs, you can specify default settings for controls, or display calculated data that depends on the data in the form's records.
By running a macro or an event procedure when a form's Unload event occurs, you can verify that the form should be unloaded or specify actions that should take place when the form is unloaded. You can also open another form or display a dialog box requesting the user's name to make a log entry indicating who used the form.
When you first open a form, the following events occur in this order:
Open Load Resize Activate Current
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.
When you close a form, the following events occur in this order:
Unload Deactivate Close
The Unload event occurs before the Close event. The Unload event can be canceled, but the Close event can't.
Note When you create macros or event procedures for events related to the Load event, such as Activate and GotFocus, be sure that they don't conflict (for example, make sure you don't cause something to happen in one macro or procedure that is canceled in another) and that they don't cause cascading events.See Also
Activate, Deactivate events, Close action, Current event, Event properties, GotFocus, LostFocus events, GoToRecord action, Open, Close events, OpenForm action, Resize event, SetFocus method.