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:
MDI form QueryUnload event
All loaded child forms' QueryUnload events
All loaded child forms' Form_Unload events
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 ...
Get VB & VBA in a Nutshell: The Language now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.