Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prompt before close

Tags:

vba

ms-access

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
like image 327
Ish Avatar asked Nov 25 '25 17:11

Ish


1 Answers

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
like image 153
DJ. Avatar answered Nov 27 '25 14:11

DJ.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!