Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Event on exiting an application

Status
Not open for further replies.

Eek

Technical User
Feb 4, 2001
34
CA
How do I get a event to start when the user exits my program by clicking the " X " button ?
 
You can declare an event (Public Event MyEvent()) in General Deeclarations and then raise the event in the Unload event of the form.
 
Oops sorry <<sheepish grin>> it's not as simple as that. Here's what you ACTUALLY have to do...

Add a class module to your form.

Put the following code in your class module.

Public Property Get Form1() As Form1
Set Form1 = mForm1
End Property

Public Property Set Form1(ByVal NewForm1 As Form1)
Set mForm1 = NewForm1
End Property

Substitute Form1 with the name of your form.

Then, in your form's code window (General declarations), declare your event - with any necessary arguments:

Public Event MyEvent (byval argument as type)

and declare a private variable to hold an instance of your class module.

Back to the code of the class module:
in General Declarations, declare a variable WithEvents as an instance of your form e.g.

Private WithEvents MyForm as Form1

You should then be able to select &quot;MyForm&quot; from the object drop down list (left hand side) in your class's code window. It should bring up a sub proceedure for your custom event - where you can add all the necessary code.

Back to the form's code

In the form_load event, instantiate your class variable (set x = new class1) and add the following code after that:

Set x.Form1 = Me

where x is the name of the variable to hold the instance of your class and Form1 is the name of your form.

You can then raise the event either from form_unload or in the button_click for your X button - depending on how you want it to work.

Set the variable holding your class variable to nothing in form_unload, after the event is called.

Sorry this is so long but it is the best way to do it.

Hope it helps you!!
 
Do you really need your own program defined event or can an existing VB form event do the job? If the latter is the case, then its probably easier to use the QueryUnload event:

Private Sub Form_QueryUnload(cancel As Integer,unloadmode As Integer)

Select Case unloadmode
Case vbFormControlMenu

'Close button (X) pushed

.....
Case Else
.....
End Select


The cancel can be set to stop ( <>0 ) or to further proceed (= 0) with the Unloading.
_________________________________
In theory, there is no difference between theory and practice. In practice, there is. [attributed to Yogi Berra]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top