Unload Statement

Syntax

Unload object


object

Use: Required

Data Type: Object

A form or control loaded at runtime.

Description

Removes a form or (in Visual Basic) a dynamically created member of a control array from memory.

Rules at a Glance

  • Only controls added to a control array at runtime can be removed from memory using the Unload statement. Those controls added to a form at design time can't be unloaded individually.

  • When a form, the form's Query_Unload event is fired. Once its event handler has executed, the form's Form_Unload event is fired.

  • When object is an MDI form, the following is the order of events:

    1. MDI form QueryUnload event

    2. All loaded child forms' QueryUnload events

    3. All loaded child forms' Form_Unload events

    4. MDI form Form_Unload event

Programming Tips and Gotchas

  • You can prevent a form from unloading by setting the Cancel argument to True in a QueryUnload or Form_Unload event procedure. Cancel is a parameter passed by reference to both the QueryUnload and the Unload events.

  • When you unload a form from within the form's code, you should use the Me keyword to refer to the form. This makes your code more readable, as the following snippet shows:

    Unload Me

  • Microsoft recommends that forms should be unloaded only in the Click event of a CommandButton or menu control. Calling the Unload statement in other event handlers can have undesirable side effects and cause general protection faults (GPFs).

  • You can also bring your code for loading and unloading forms in line with the latest object coding in Visual Basic. The following snippet shows the modern alternative to using the Load and Unload statements for a Form object:

    Dim frmVar as Form1
    Set frmVar = New Form1
    ...
    frmVar.Show vbModal
    ...
    Set frmVar = Nothing

    This requires, of course, that a form named Form1 be included in the project, and that it not be designated as the startup module.

  • When you unload a form from memory, only the form window and controls are unloaded; the code attached to a form and controls within the form module remain in memory.

See Also

Load Statement
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.58.121.251