February 28, 2021
Hot Topics:

Unloading All Forms in Visual Basic

  • By John Percival
  • Send Email »
  • More Articles »

How do you close your Visual Basic program? Do you use the End method? In this tip I will explain why using the End method is not the best way to close your program. It is an undocumented fact that using the End method does not completely remove all forms from memory. This means that your program does not give up all resources to other processes when it is closed using the end method. The only way to completely shut down the program is to unload each of the forms using the Unload method. Here is some code that loops through all loaded forms and unloads them one by one. As soon as no forms are loaded, your program has been closed.

Public Sub UnloadAllForms()
Dim Form As Form
   For Each Form In Forms
      Unload Form
      Set Form = Nothing
   Next Form
End Sub

It is best to call this procedure from the unload event of your main form.

This article was originally published on November 19, 2002

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date