Unload Statement

See AlsoWGTC6N              Example0LTZQA>Low

Unloads a form5272EF or controlXX9KJT from memory.

Syntax

Unload object

Remarks

The argument object is the form or control array element to unload.

Unloading a form or control may be necessary or expedient in some cases where the memory used is needed for something else or when you need to reset properties to their original value.

Before a form is unloaded, the Query_Unload event occurs, followed by the Form_Unload event.  Setting the Cancel argument to True in either of these events keeps the form from being unloaded.  For MDI forms, the MDI form Query_Unload event occurs, followed by the Query_Unload event and Form_Unload event for each MDI child form, and finally the MDI form Form_Unload event

When a form is unloaded, all controls placed on the form at run timeCYRM35 are no longer accessible.  Controls placed on the form at design timeCFSL0V remain intact, although any run-time changes to those controls and their properties are lost when the form is reloaded.  All changes to form properties are also lost.  Note that when a form is unloaded, only the displayed component is unloaded.  The code associated with the form module remains in memory.

Only control array elements added to a form at run-time can be unloaded with the Unload statement.  The properties of unloaded controls are reinitialized when the controls are reloaded.


See Also

Hide MethodGU6180

Load StatementGU0759

QueryUnload Event0HC58S7

Show Method2IF9Z3Q


Unload Statement Example

The example uses the Unload statement to unload a form.  To try this example, paste the code into the Declarations section of a form named Form1.  Then press F5 and click the form.

 

Sub Form_Click ()

   Dim Answer, Msg                       ' Declare variables

   Unload Form1                          ' Unload form.

   Msg = "Form1 has been unloaded. Choose Yes to load and "

   Msg = Msg & "display the form. Choose No to load the form "

   Msg = Msg & "and leave it invisible."

   Answer = MsgBox(Msg, 4)               ' Get user response.

   If Answer = 6 Then                    ' Evaluate answer.

      Show                               ' If Yes, show form.

   Else

      Load Form1                         ' If No, just load it.

      Msg = "Form1 is now loaded. Choose OK to display it."

      MsgBox Msg                         ' Display message.

      Show                               ' Show form.

   End If

End Sub