Bypass QueryClose on Form

AlexanderBB

Well-known Member
Joined
Jul 1, 2009
Messages
1,940
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
I want to use the Form X Close event and btnSave event to run the same prompt e.g
VBA Code:
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
    Select Case MsgBox("Save changes", vbYesNoCancel + vbQuestion)
        Case vbYes
            Stop
        Case vbNo
           Unload Me
        Case Else
            Cancel = True
    End Select
End Sub

Private Sub btnSave_Click()
    UserForm_QueryClose 0, 0
End Sub

The problem is "Unload Me' runs the QueryClose again.
What might be the correct way to handle this? Is CloseMode an option ?
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Sorted.. used a boolean. Unless a 'better' way?
You don't need to call QueryClose even procedure, it will be triggered as soon as the form is closed anyway.
Try the following code:

VBA Code:
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
    Select Case MsgBox("Save changes", vbYesNoCancel + vbQuestion)
        Case vbYes
            MsgBox "Saving the changes first..."
        Case vbNo
            ' This condition is not necessary, just for testing purposes
            MsgBox "Closing without saving the changes..."
        Case vbCancel
            Cancel = True
    End Select
End Sub

Private Sub btnSave_Click()
    Unload Me
End Sub
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,119
Members
451,398
Latest member
rjsteward

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top