Edit

Share via


Prompt a user before saving a record

The following example illustrates how to use the BeforeUpdate event to prompt users to confirm their changes each time they save a record in a form.

Private Sub Form_BeforeUpdate(Cancel As Integer) 
   Dim strMsg As String 
   Dim iResponse As Integer 
 
   ' Specify the message to display. 
   strMsg = "Do you wish to save the changes?" & Chr(10) 
   strMsg = strMsg & "Click Yes to Save or No to Discard changes." 
 
   ' Display the message box. 
   iResponse = MsgBox(strMsg, vbQuestion + vbYesNo, "Save Record?") 
    
   ' Check the user's response. 
   If iResponse = vbNo Then 
    
      ' Undo the change. 
      DoCmd.RunCommand acCmdUndo 
 
      ' Cancel the update. 
      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.