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!

Determine if Close Button is Used to Close Form 1

Status
Not open for further replies.
Is there a way to determine if a user uses the “Close Button”, the “X” in the upper right-hand corner, to initiate a close form in VBA. I would like to initiate certain tasks if the “Close Button” is used instead of a command button on the form that will do other things before closing. I was going to remove the close button and force the use of a command button - but I am trying to appease a user.

Thanks much for your input.



Moehle
 
I was going to remove the close button" - bad idea.
"but I am trying to appease a user." - kudos, this is a lot better solution.

Code:
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)

Select Case CloseMode
    Case vbFormControlMenu
        MsgBox "The user has chosen the Close command from the Control menu on the UserForm."
    Case vbFormCode
        MsgBox "The Unload statement is invoked from code."
    Case vbAppWindows
        MsgBox "The current Windows operating environment session is ending."
    Case vbAppTaskManager
        MsgBox "The Windows Task Manager is closing the application."
End Select

End Sub

More about CloseMode here

---- Andy

"Hmm...they have the internet on computers now"--Homer Simpson
 
Not my question, but happened upon it. Thanks, Andy, for that detailed response.

"But thanks be to God, which giveth us the victory through our Lord Jesus Christ." 1 Corinthians 15:57
 
Just to let you know, MsgBox is not the best method to test all of those posibilities.
Yes, vbFormControlMenu and vbFormCode will give you a message box, but vbAppWindows and vbAppTaskManager will not because you just killed the app from 'outside' and message boxes will not appear. But whatever logic you place in there will still run :)

---- Andy

"Hmm...they have the internet on computers now"--Homer Simpson
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top