Form.Unload event (Access)

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.

Syntax

expression.Unload (Cancel)

expression A variable that represents a Form object.

Parameters

Name Required/Optional Data type Description
Cancel Required Integer Set to True to cancel the Unload event.

Remarks

To run a macro or event procedure when these events occur, set the OnUnload property to the name of the macro or to [Event Procedure].

The Unload event is caused by user actions such as:

  • Closing the form.
  • Running the Close action in a macro.
  • Quitting an application by right-clicking the application's taskbar button, and then choosing Close.
  • Quitting Windows while an application is running.

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 close a form, the following events occur in this order:

UnloadDeactivateClose

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 Unload event, such as Deactivate and LostFocus, be sure that they don't conflict (for example, make sure that 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.

Example

This example prompts the user to verify that the form should close.

To try the example, add the following event procedure to a form. In Form view, close the form to display the dialog box, and then choose Yes or No.

Private Sub Form_Unload(Cancel As Integer) 
 If MsgBox("Close form?", vbYesNo) = vbYes Then 
 Exit Sub 
 Else 
 Cancel = True 
 End If 
End Sub

Support and feedback

Have questions or feedback about Office VBA or this documentation? Please see Office VBA support and feedback for guidance about the ways you can receive support and provide feedback.