On CNET: Prez-elect Obama 60 Min. interview

VB6 Tip: Best practices requires that you ensure all forms unload

Tags: Microsoft development tools, Programming languages, Peter Aitken, Microsoft Visual Basic 6.0, Peter Aitken, Microsoft Visual Basic, main form, Visual Basic Tips Newsletter

  • Save
  • Print
  • Recommend
  • 7

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 Form
For 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.Name

It 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!

  • Save
  • Print
  • Recommend
  • 7

Print/View all Posts Comments on this article

ensure all forms unloademmbe1@...  | 01/17/05
Unloading all forms?HenryStinson@...  | 01/21/05
Forms collection misapprehensionjohnwm  | 03/25/05
Is Nothing can cause Problems tooismith@...  | 04/13/07
Unloading formsae@...  | 11/04/05
Bad coding practice to begin withjreddy@...  | 02/01/07

What do you think?

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

All-in-One Printers

advertisement
Click Here