Occurs before a form or application closes.
Private Sub Form_QueryUnload(cancel, unloadmode)
The QueryUnload event syntax has these parts:
Part | Description |
---|---|
cancel | An integer. Setting this argument to any value other than 0 stops the QueryUnload event in all loaded forms and stops the form and application from closing. |
unloadmode | A value or constant indicating the cause of the QueryUnload event, as described in Return Values. |
The unloadmode argument returns the following values:
Constant | Value | Description |
---|---|---|
vbFormControlMenu | 0 | The user chose the Close command from the Control menu on the form. |
vbFormCode | 1 | The Unload statement is invoked from code. |
vbAppWindows | 2 | The current Microsoft Windows operating environment session is ending. |
vbAppTaskManager | 3 | The Microsoft Windows Task Manager is closing the application. |
This event is typically used to make sure there are no unfinished tasks in the forms included in an application before that application closes. For example, if a user has not yet saved some new data in any form, your application can prompt the user to save the data.
When an application closes, you can use either the QueryUnload or Unload event procedure to set the Cancel property to True, stopping the closing process. However, the QueryUnload event occurs in all forms before any are unloaded, and the Unload event occurs as each form is unloaded.