Preventing a form from unloading
By: Unknown Date: 24/09/1998

How often have you wanted to keep a user from unloading a form through the control-box? For that matter, how many times have you been frustrated because Windows shut down without your program cleaning up behind itself? Well, no more.

The trick, of course, is in the QueryUnload event for the given form. Any time your form is about to be unloaded, Windows triggers this event within your program to basically "ask permission" to terminate it. Windows will also provide a little information. If you want your program to shut down quietly if your program is attempting to close it, but not the Task Manager. There are 5 trappable ways a form can be unloaded. They are:

  1. The user has chosen the Close command from the Control-menu box on the form.
  2. The Unload instruction has been invoked from code.
  3. The current Windows environment session is ending.
  4. The Microsoft Windows Task Manager is closing the application.
  5. An MDI child form is closing because the MDI form is closing.

Below is the routine to determine how the program is being unloaded. In the example below, the only way the form can be unloaded is if the program itself has unloaded it.

Sub Form_QueryUnload (Cancel As Integer, UnloadMode As Integer)
    On Error Resume Next

    If UnloadMode <> 1 Then
Cancel = True
    End If

End Sub

Click Here!


Visual Basic Programming Zone is a website by Lorenzo Dalla Vecchia.
Webmaster's e-mail: webmaster@vbprogzone.cjb.net
Hosted by InWind: www.inwind.it