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 form
s, 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)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":

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).