See Also Example
Applies To
Form, MDI form
Description
Occurs before a form or application closes. When an MDI form closes, the QueryUnload event occurs first in the MDI form and then in all child forms. If no form cancels the QueryUnload event, the Unload event then occurs first in all other forms and last in an MDI form. When a child form or a non-MDI form closes, the QueryUnload event in that form occurs before the form's Unload event.
Syntax
Sub Form_QueryUnload (Cancel As Integer, UnloadMode As Integer)
Sub MDIForm_QueryUnload (Cancel As Integer, UnloadMode As Integer)
Remarks
The QueryUnload event uses the following arguments:
Argument Description
Cancel Setting this argument to any value other than zero stops the QueryUnload event in all loaded forms and stops the form and application from closing.
UnloadMode Indicates the reason for the QueryUnload event.
The UnloadMode argument can have these values:
Value Description
0 The user has chosen the Close command from the Control-menu box on the form.
1 The Unload method has been invoked from code.
2 The current Windows-environment session is ending.
3 The Microsoft Windows Task Manager is closing the application.
4 An MDI child form is closing because the MDI form is closing.
This event is typically used to make sure there are no unfinished tasks in each form before an application closes. For example, if a user has not yet saved some new data in any form, your application can ask the user if the data should be saved.
When an application closes, you can use either QueryUnload or Unload However, the QueryUnload event occurs in all forms before any are unloaded, and the Unload event occurs as each form is unloaded.
Help:
Unload Event
Unload Statement
Programmer's Guide:
Chapter 14, "Multiple-Document Interface (MDI) Applications"
The example uses an MDI form containing two MDI child forms. When you choose the Close command from the Control-menu box to close a form, you get a different message than if you choose the Exit command from the File menu. To try this example, create an MDI form and then use the Menu Design window to create a File menu containing an Exit command named FileExit. On Form1, set the MDIChild property to True. Paste the code into the Declarations sections of the respective forms and press F5 to run the program.
' Paste into Declarations section of MDIForm1
Sub MDIForm_Load ()
Dim NewForm As New Form1 ' New instance of Form1.
NewForm.Caption = "Form2" ' Set caption and show.
End Sub
Sub FileExit_Click ()
Unload MDIForm1 ' Exit the application.
End Sub
Sub MDIForm_QueryUnload (Cancel As Integer, UnloadMode As Integer)
Dim Msg ' Declare variable.
' Set the message text.
Msg = "Do you really want to exit the application?"
' If user clicks the 'No' button, stop QueryUnload.
If MsgBox(Msg, 36, Me.Caption) = 7 Then Cancel = True
End Sub
' Paste into Declarations section of Form1
Sub Form_QueryUnload (Cancel As Integer, UnloadMode As Integer)
Dim Msg ' Declare variable.
If UnloadMode > 0 Then
' If exiting the application.
Msg = "Do you really want to exit the application?"
Else
' If just closing the form.
Msg = "Do you really want to close the form?"
End If
' If user clicks the 'No' button, stop QueryUnload.
If MsgBox(Msg, 36, Me.Caption) = 7 Then Cancel = True
End Sub