VB6 beginners tutorial - Learn VB6

Advanced VB6 tutorial - Learn Advanced VB6

Systems Analysis - System analysis and Design tutorial for Software Engineering

You are here: Visual Basic > Advanced VB6 tutorial > Chapter 6

DeActivate, Unload, QueryUnload, and Terminate Events

These events fire toward the end of a form's life. Their event procedures are therefore good places to put "cleanup" code. Once again, the exact circumstances and timing of these events can differ.

The DeActivate Event

The DeActivate event fires when a form or a control on the form loses focus to another object in the application outside the form. The DeActivate event does not fire when the user navigates to a completely different application, but only when the user navigates elsewhere in the current application. The DeActivate event does not fire when the application closes or when the current form unloads.

NOTE : Be Careful With the End Statement. You can quickly terminate your application by putting a one-word statement, "End", in your code. However you should be aware that doing so will abruptly stop any further processing by the application.

In particular this means that loaded forms' QueryUnload, Unload, and Terminate events will not run. If you've put code in these event procedures to perform cleanup on the environment or save pending data, this code will not run when you call End.

Pressing the VCR stop button on the VB menu in order to stop your designtime application is equivalent to calling the End statement.

The QueryUnload Event

The QueryUnload event fires just before a form unloads from memory. The QueryUnload event fires just before the Unload event. Its main purpose is to let you detect why the form is being unloaded and to programmatically halt unloading if necessary. The QueryUnload event procedure takes two parameters:

1. Cancel This is a True/False value which is False by default. When Cancel is False, it means that the unloading will continue. You can set it to True to stop the form from unloading.

2. UnloadMode This parameter can take several values, corresponding to how the QueryUnload event was triggered. You can compare UnloadMode's value with one of the following VB internal constants:

  • vbformControlMenu. The form's QueryUnload event was triggered because the user is closing the form.

  • vbformCode. The form's QueryUnload event was triggered by code that programmatically closes the form.

  • vbAppWindows. The QueryUnload event was triggered because the Windows session is ending.

  • vbAppTaskManager. The QueryUnload event was triggered because the Windows Task Manager is closing your application.

  • vbformMDIform. The form is an MDI Child, and the MDI Parent is closing.

A common use of the QueryUnload event is to prompt the user to save changes (see Figure 6.2).

A typical use of the QueryUnload event is to give users a chance to change their minds
FIGURE 6.2 A typical use of the QueryUnload event is to give users a chance to change their minds.

You give the user the option to cancel the unload. If the user chooses to cancel, you can set the Cancel parameter to True, as in Listing 6.1.


Private Sub frmData_QueryUnload(Cancel as Integer, _
UnloadMode As Integer)
Dim intUserChoice As Integer
intUserChoice = MsgBox("Save Changes?" , _
If intUserChoice = vbYes Then
Call SaveData
ElseIf intUserChoice = vbCancel Then
Cancel = True
End If
End Sub

The Unload Event

The Unload event procedure is where programmers usually put cleanup code. The Unload event fires after the QueryUnload event. The Unload event procedure takes a single parameter, the Cancel parameter. Unload's Cancel parameter works the same as QueryUnload's Cancel parameter.

It is possible to stop the form from unloading in the Unload event procedure by setting Cancel to True. However, since the Unload event doesn't receive the QueryUnload event's UnloadMode parameter, your Unload event procedure has less information about why the form is being unloaded than the QueryUnload event procedure has.

For forms which are not MDI Child forms, Unload always happens immediately after QueryUnload (unless, of course, the unloading was just cancelled in the QueryUnload by setting the Cancel parameter to True).

NOTE : Timing of QueryUnload and Unload in MDI Applications. In an MDI application, MDI Child forms have a slightly different timing for QueryUnload and Unload events. See the section in this chapter titled "Using the Unload and QueryUnload Events in an MDI Application" for more information

The Terminate Event

The Terminate event happens when the instance of the form has been completely unloaded and all the form's variables have been set to Nothing. The Terminate event happens only when you explicitly set the form to Nothing in your code during or after the Unload event procedure. For example, you could use the statement:

Set Form1 = Nothing

after calling the Unload statement for Form1.

You might use the Terminate event to perform final cleanup operations for your form.


<< Previous | Contents | Next >>

Home | About Us | Privacy Policy | Contact Us

Copyright © | All Rights Reserved