Unload Event

See AlsoLV53Q9              Example5YS09Q5>Low

Applies To

Form14TJ2LN, MDI Form1ILMZQ7.


Occurs when a form is about to be removed from the screen.  When that form is reloaded, the contents of all its controls are reinitialized.  This event is triggered by user action (closing the form using the Control-menu box), or by an Unload17FKE8Z statement.


Sub Form|MDIForm}_Unload (Cancel As Integer)


Setting Cancel to any nonzero value prevents the form from being removed, but does not stop other events such as exiting from Windows.  Use the QueryUnload event to stop exiting from Windows.

Use an Unload event procedure to verify that the unloading of the form should proceed or to specify actions that should take place when the form is unloaded.  You can also include any form-level validation code you might need for closing the form or saving the data in it to a file.

The QueryUnload0HC58S7 occurs before the Unload.

The Unload event can be caused by using the Unload17FKE8Z statement, or by the user taking an action such as choosing the Close command on a form's Control menu, exiting the application with End Task button on the Windows Task Manager, closing the MDI form for which the current form is a child form, or exiting the Windows environment while the application is running.

See Also


Load Event2S1GYBF

Load StatementGU0759

QueryUnload Event0HC58S7

Unload Statement17FKE8Z


Programmer's Guide:

Chapter 18, "Using the File-System Controls"

Unload Event Example

The example demonstrates a simple procedure to close a form while querying the user with various message boxes.  In an actual application, you could add calls to general-purpose Sub procedures that emulate the processing of the Exit, Save, and Save As commands on Visual Basic's File menu.  To try this example, paste the code into the Declarations section of a form.  Then press F5.  Once the form is displayed, press Alt + F4 to close the form.

Sub Form_Unload (Cancel As Integer)

   Dim Msg, Response                     ' Declare variables.

   Msg = "Save Data before closing?"

   Response = MsgBox(Msg, 3 + 32, "Save Dialog")

   Select Case Response

      Case 2                             ' Do not allow close.

         Cancel = -1

         Msg = "Command has been cancelled."

      Case 6

      ' Enter code to save data here.

         Msg = "Data saved."'

      Case 7

         Msg = "Data not saved."

   End Select

   MsgBox Msg, 0, "Confirm"              ' Display message.

End Sub