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 derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Close form without triggering Exit event of control??? 1

Status
Not open for further replies.

VBAjedi

Programmer
Dec 12, 2002
1,197
KH
Hi! I have several controls on my VB form that have code attached to their Exit event. Apparently, if one of these controls has the focus when the user closes the form, it triggers the Exit event for that control, which runs the code and causes errors because it is referring to objects which are no longer loaded!!

Is there a way to avoid triggering the Exit event in that case? I played around with UserForm_QueryClose but couldn't come up with a satisfactory solution. Creating my own "Close Form" button results in the same problem, because the Exit event code still runs for the control with the focus. And yes, the code needs to be attached to the Exit event!

Thanks for your help!

VBAjedi
 
I've run into this, and have resorted to setting a module variable "Closing" to true in the OKbutton_click event (which, in my case, closes the form), to signify that the exit event no longer needs to run. In the _exit handler, just start with

if Closing then exit sub

there may be more elegant solutions, but that works for me.
Rob
[flowerface]
 
That seems to have done it. It's a bit awkward, but it runs!

Thanks again, Rob!

VBAjedi
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top