VB6 Tip: Best practices requires that you ensure all forms unload
Takeaway: Writing proper code requires that when a VB program ends, all of its forms will be unloaded and removed from memory.
When a Visual Basic 6 program ends, all of its forms should be unloaded and removed from memory. VB doesn't do this automatically; therefore, if your program contains a lot of forms, it's possible for one or more forms to remain in memory even after the program terminates.
Calling a form's Hide method or setting its Visible property to False doesn't unload it. Even if you explicitly unload a form (using the Unload procedure), it can still take up resources—unless the form's reference is set to Nothing. This tip shows how to ensure that all of the forms in a program unload and that their resources release upon program termination.
This technique makes use of the fact that a VB application has a global Forms collection whose elements represent all of the application's loaded forms.
You could loop through this collection, unloading all forms as shown in this code snippet:
Dim f As FormFor Each f In Forms
Unload f
Set f = Nothing
Next f
There's a problem with this straightforward approach: If you execute this code from a procedure, it works fine; but if you call it from the main form's Form_Unload event procedure, it will try to unload the main form, which is already in the process of unloading itself (otherwise the Form_Unload event procedure wouldn't have fired). Here's how you can get around this potential problem:
Public Sub UnloadAllForms(Optional FormToIgnore _As String = "")
Dim f As Form
For Each f In Forms
If f.Name <> FormToIgnore Then
Unload f
Set f = Nothing
End If
Next f
End Sub
With this procedure in place, you'd call it like this from the main form's Form_Unload event procedure:
UnloadAllFormsMe.NameIt will unload all forms except the main form.
If calling it from a separate procedure, don't pass an argument, and the procedure will unload all of the program's forms.
Advance your scripting skills to the next level with TechRepublic's free Visual Basic newsletter, delivered each Friday. Automatically sign up today!
Print/View all Posts Comments on this article
SponsoredWhite Papers, Webcasts, and Downloads
- VMware DRS: Why You Still Need Assured Application Delivery and Application Delivery Networking F5 Networks
- Design Firm Raises Per-Employee Revenue by 25 Percent with Collaboration Solution Microsoft
- Deep Talent, Vast Distances: Realizing the full value of global knowledge workers Deloitte LLP
- An Overview of Microsoft Exchange Server 2007 White Paper Microsoft
- Web 2.0 for the Enterprise: Setting the Foundation for Success Oracle
Article Categories
- Security
- Security Solutions, IT Locksmith
- Networking and Communications
- E-mail Administration NetNote, Cisco Routers and Switches
- CIO and IT Management
- Project Management, CIO Issues, Strategies that Scale
- Desktops, Laptops & OS
- Windows 2000 Professional, Microsoft Word, Microsoft Excel, Microsoft Access, Windows XP,
- Data Management
- Oracle, SQL Server
- Servers
- Windows NT, Linux NetNote, Windows Server 2003
- Career Development
- Geek Trivia
- Software/Web Development
- Web Development Zone, Visual Basic, .NET

