共用方式為


How to: Perform Simple Data Validation Checks When Editing a Record in a Form

You can 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