One kind of question that repeatedly gets asked on the forums is about properly (or completely) closing a form/class, or an entire program.

The guidelines below will hopefully cover everything you need to know.. but don't be put off by the length of this article, as most of it is just explainations and examples of the bullet points in this first post (you can skip sections that don't apply to you). Depending on your program, closing a form/class/program may be just a couple of lines of code!

If after following this guide you find that something is not unloading properly (ie: in VB itself you need to press the "stop" button, or when compiled your program is still listed in Task Manager), see the last post in this thread, as it explains how to find the issue.

Closing a form
A form will start to close when the user presses the "X" button on the titlebar, or you tell it to close, or one of a few other reasons (such as Task Manager is closing your program).

If you want to tell a form to close (such as in an "Exit" button you add to the form), use the Unload statement, eg:
Code:
Unload Form1        'tell the form to unload
Set Form1 = Nothing 'free the memory used by the variables
Note that if this code is in the form itself, you should add "Exit Sub" (or "Exit Function") immediately afterwards, so that no more code runs (otherwise any code that refers to the form or its controls/properties will re-load it!).

Events that occur during the unloading process
When a form starts unloading, the Form_QueryUnload event will fire, and you can use this to cancel the unload process of your form (or forms, if multiple are closing for the same reason). For details of how to do that, see the article How can I show a confirmation message when my form is closing?. If the process is Cancelled, the form(s) will stay open, and no more events will fire.

Next the Form_Unload event will fire, and this is the recommended place to put your tidy up code, as it does not interfere with the _QueryUnload process (which is important if you have multiple forms, and particularly so for MDI applications), and does not have the problems which the next event has.

There is also a Form_Terminate event, but using it for your tidy up process is not recommended as it fires after most visual aspects of the form have unloaded, so if you use that, you need to be much more careful what code you use - otherwise the form will re-load!

Note that if you use the Unload statement, all three of these events will fire before the next line of code runs (in my example above, the "Set" line).

Making sure the form is unloaded properly
No matter how the closing of the form was started (via code, the user pressing the "X" button on the titlebar, etc), you should ensure that you tidy up properly - if you don't, the form might not actually unload.

To tidy up, you should perform the following steps in the Form_Unload event:
(these links point to sections of this page with more details - if you click the links, you can press your Back button to return to this list)

Properly closing a class
A class will automatically start to close when you explicitly set the variable you used to Nothing (eg: "Set MyClassVariable = Nothing"), or when the variable goes out of scope.

When this happens, the Class_Terminate event will fire. In this event you should perform the following steps:
(these links point to sections of this page with more details - if you click the links, you can press your Back button to return to this list)

Closing your entire program
Several people use (or even recommend) using the "End" statement to close a program, however there are many reasons why you should not do that, as explained in the article Why is using the 'End' statement (or VB's "stop" button) a bad idea?.

The correct method is briefly explained in the help for "End":
Quote Originally Posted by VB's help for the End statement
For normal termination of a Visual Basic program, you should unload all forms. Your program closes as soon as there are no other programs holding references to objects created from your public class modules and no code executing.
Here is a more detailed version of that:
(these links point to sections of this page with more details - if you click the links, you can press your Back button to return to this list)

Don't forget that if you are using code to initiate the close process, you need to add "Exit Sub" or "Exit Function" after it, so that no code after that point runs (as that is likely to keep your program open).