Edit

Share via


Prompt a user before closing a form

The following example illustrates how to prompt the user to verify that the form should be closed.

Private Sub Form_Unload(Cancel As Integer) 
 If MsgBox("Are you sure that you want to close this form?", vbYesNo) = vbYes Then 
 Exit Sub 
 Else 
 Cancel = True 
 End If 
End Sub

Support and feedback

Have questions or feedback about Office VBA or this documentation? Please see Office VBA support and feedback for guidance about the ways you can receive support and provide feedback.