Reputation: 671
Before the user closes the form I want to prompt with a confirmation box.
I am not sure how to do this. I have tried the code below and it prompts the user but upon clicking no it closes the form anyway:
Private Sub Form_Close()
If MsgBox("Test", vbYesNo + vbExclamation, "Confirm close") = vbYes Then
Else
Cancel = True
End If
End Sub
Upvotes: 3
Views: 3473
Reputation: 16257
You can't cancel the close event but you can cancel the unload event
Private Sub Form_Unload(Cancel As Integer)
If MsgBox("Test", vbYesNo + vbExclamation, "Confirm close") <> vbYes Then
Cancel = True
End If
End Sub
Upvotes: 6