<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="VB6Parse Library Reference - unload - System Interaction">
<title>unload - System Interaction - VB6Parse Library Reference</title>
<link rel="stylesheet" href="../../../assets/css/style.css">
<link rel="stylesheet" href="../../../assets/css/docs-style.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/styles/github-dark.min.css">
<script src="../../../assets/js/theme-switcher.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/highlight.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/languages/vbnet.min.js"></script>
<script>hljs.highlightAll();</script>
</head>
<body>
<header class="docs-header">
<div class="container">
<h1><a href="../../../index.html">VB6Parse</a> / <a href="../../../library/index.html">Library</a> / <a href="../../../library/statements/system_interaction/index.html">System Interaction</a> / unload</h1>
<p class="tagline">VB6 Library Reference</p>
</div>
</header>
<nav class="docs-nav">
<div class="container">
<a href="../../../index.html">Home</a>
<a href="../../../library/index.html">Library Reference</a>
<a href="../../../documentation.html">Documentation</a>
<a href="https://docs.rs/vb6parse" target="_blank">API Docs</a>
<a href="https://github.com/scriptandcompile/vb6parse" target="_blank">GitHub</a>
<button id="theme-toggle" class="theme-toggle" aria-label="Toggle theme">
<span class="theme-icon">🌙</span>
</button>
</div>
</nav>
<main class="container">
<article class="library-item">
<h1 id="unload-statement">Unload Statement</h1>
<p>Removes a form or control from memory.</p>
<h2 id="syntax">Syntax</h2>
<pre><code class="language-vbnet">Unload object</code></pre>
<h2 id="parts">Parts</h2>
<ul>
<li><strong>object</strong>: Required. An object expression that evaluates to a Form or control. If object is
a form, unloading the form causes all controls on the form to be unloaded as well.</li>
</ul>
<h2 id="remarks">Remarks</h2>
<ul>
<li><strong>Form Unloading</strong>: When a form is unloaded, all of its controls are removed from memory and
all values of the form's properties are lost. You can use the <code>Hide</code> method to make a form
invisible without unloading it, allowing you to continue to access properties of the form
and its controls.</li>
<li><strong>Control Arrays</strong>: When you unload a control created at run time with the <code>Load</code> statement,
the control is removed from the control array, and the array's upper bound is decremented by one.</li>
<li><strong><code>QueryUnload</code> Event</strong>: Before a form is unloaded, the <code>QueryUnload</code> event procedure is called.
Setting the <code>Cancel</code> argument to <code>True</code> in the <code>QueryUnload</code> event prevents the form from
being unloaded.</li>
<li><strong>Unload Event</strong>: After the <code>QueryUnload</code> event, the <code>Unload</code> event procedure is called. You
can include code in this event procedure to save data or clean up resources.</li>
<li><strong>Me Keyword</strong>: Within a form's code, you can use <code>Unload Me</code> to unload the form itself.</li>
<li><strong>Subsequent References</strong>: Any subsequent references to properties or controls on an unloaded
form will cause the form to be reloaded and its <code>Load</code> event to fire.</li>
</ul>
<h2 id="examples">Examples</h2>
<h3 id="simple-form-unload">Simple Form Unload</h3>
<pre><code class="language-vbnet">Unload Form1</code></pre>
<h3 id="unload-current-form">Unload Current Form</h3>
<pre><code class="language-vbnet">Private Sub cmdClose_Click()
Unload Me
End Sub</code></pre>
<h3 id="unload-control-array-element">Unload Control Array Element</h3>
<pre><code class="language-vbnet">Unload txtDynamic(5)</code></pre>
<h3 id="unload-with-cleanup">Unload With Cleanup</h3>
<pre><code class="language-vbnet">Private Sub Form_Unload(Cancel As Integer)
' Save data before closing
SaveSettings
CloseDatabase
End Sub</code></pre>
<h3 id="conditional-unload">Conditional Unload</h3>
<pre><code class="language-vbnet">If UserConfirmed Then
Unload frmDialog
End If</code></pre>
<h2 id="common-patterns">Common Patterns</h2>
<h3 id="save-data-before-unload">Save Data Before Unload</h3>
<pre><code class="language-vbnet">Private Sub Form_Unload(Cancel As Integer)
If DataModified Then
Dim response As VbMsgBoxResult
response = MsgBox("Save changes?", vbYesNoCancel)
If response = vbYes Then
SaveData
ElseIf response = vbCancel Then
Cancel = True ' Prevent unload
End If
End If
End Sub</code></pre>
<h3 id="unload-multiple-forms">Unload Multiple Forms</h3>
<pre><code class="language-vbnet">Sub CloseAllForms()
Dim frm As Form
For Each frm In Forms
If frm.Name <> "frmMain" Then
Unload frm
End If
Next frm
End Sub</code></pre>
<h3 id="unload-dynamically-created-controls">Unload Dynamically Created Controls</h3>
<pre><code class="language-vbnet">Dim i As Integer
For i = 1 To 10
Unload lblDynamic(i)
Next i</code></pre>
<h2 id="best-practices">Best Practices</h2>
<ol>
<li><strong>Use Unload vs Hide</strong>: Use <code>Unload</code> when you're done with a form and want to free memory.
Use <code>Hide</code> when you want to make a form invisible but may need to show it again soon.</li>
<li><strong>Clean Up Resources</strong>: Use the <code>Unload</code> event to close database connections, release objects,
and perform other cleanup tasks.</li>
<li><strong>Prevent Accidental Closes</strong>: Use the <code>QueryUnload</code> event with <code>Cancel = True</code> to prevent
forms from being unloaded when necessary.</li>
<li><strong>Main Form Considerations</strong>: Unloading the startup form (main form) terminates the application
unless you've specified a Sub Main procedure.</li>
<li><strong>Memory Management</strong>: Unloading forms and controls frees memory, which is important in
applications that create many forms or controls dynamically.</li>
</ol>
<h2 id="important-notes">Important Notes</h2>
<ul>
<li>Unloading a form removes it from memory completely</li>
<li>Any data stored in form-level variables is lost</li>
<li>Controls on an unloaded form cannot be accessed</li>
<li>The <code>Unload</code> event fires before the form is actually removed</li>
<li>MDI child forms are unloaded when the MDI parent is unloaded</li>
<li>You cannot unload a control that wasn't created with the <code>Load</code> statement</li>
</ul>
<h2 id="see-also">See Also</h2>
<ul>
<li><code>Load</code> statement (loads a form or control into memory)</li>
<li><code>Show</code> method (displays a form)</li>
<li><code>Hide</code> method (hides a form without unloading)</li>
<li><code>QueryUnload</code> event (fires before a form is unloaded)</li>
<li><code>Unload</code> event (fires when a form is being unloaded)</li>
</ul>
<h2 id="references">References</h2>
<ul>
<li><a href="https://learn.microsoft.com/en-us/office/vba/language/reference/user-interface-help/unload-statement">Microsoft Docs: Unload Statement</a></li>
</ul>
</article>
<div style="margin-top: 3rem; padding-top: 2rem; border-top: 1px solid var(--border-color);">
<p>
<a href="index.html">← Back to System Interaction</a> |
<a href="../index.html">View all statements</a>
</p>
</div>
</main>
<footer>
<div class="container">
<p>© 2024-2026 VB6Parse Contributors. Licensed under the MIT License.</p>
</div>
</footer>
</body>
</html>