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 "MyForm" 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.
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(cancelAs 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]
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.