PDA

View Full Version : User form "X" button to call Exit button



sujittalukde
02-04-2012, 03:37 AM
This is the code I am using for unloading the user form. The button is named as cmdExit.


Private Sub cmdExit_Click()
Dim Smsg As String
Smsg = "Are you really want to exit? Click Yes to terminate or No to Continue."
If MsgBox(Smsg, vbYesNo + vbDefaultButton2 + vbQuestion, "Exit!") = vbYes Then
Unload Me
End If
End Sub


now if the user clicks the cross "X" button on User form title bar, the form unloads without the warning message as set out in the exit button. So I put the following code:


Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
If CloseMode = vbFormControlMenu Then
Cancel = True
Call cmdExit_Click
End If
End Sub


Now the warning message is coming but the user form does not unload if the "Yes" option is clicked in the warning message.

How to overcome the issue?

Bob Phillips
02-04-2012, 03:58 AM
Private Sub cmdExit_Click()
If ExitAsk = vbYes Then Unload Me
End Sub

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
If CloseMode = vbFormControlMenu Then
If Not ExitAsk = vbYes Then Cancel = True
End If
End Sub

Private Function ExitAsk() As VbMsgBoxResult
Dim Smsg As String

Smsg = "Are you really want to exit? Click Yes to terminate or No to Continue."
ExitAsk = MsgBox(Smsg, vbYesNo + vbDefaultButton2 + vbQuestion, "Exit!")
End Function

sujittalukde
02-04-2012, 04:02 AM
Thank you xld, its working great.