Perform simple data validation checks when editing a record in a form

Use the BeforeUpdate event of a form or a control to perform validation checks on data entered into a form or control. If the data in the form or control fails the validation check, you can set the BeforeUpdate event's Cancel argument to True to cancel the update.

The following example prevents the user from saving changes to the current record if the Unit Cost field does not contain a value.

Private Sub Form_BeforeUpdate(Cancel As Integer) 
 
   ' Check for a blank value in the Unit Cost field. 
    If IsNull(Me![Unit Cost]) Then 
 
       ' Alert the user. 
       MsgBox "You must supply a Unit Cost."   
 
      ' 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.