How to: Cancel the Deletion of a Record by Using Custom Criteria

The following example illutrates how to use a form's Delete event to prevent the deletion of a record based on custom criteria. In this example, the Delete event is canceled if the value of the DataRequired field is Yes.

Private Sub Form_Delete(Cancel As Integer) 
 
   ' Check the value of the DataRequired field. 
    If Me.DataRequired = "Yes" Then 
 
      ' Cancel the record deletion. 
      Cancel = True 
 
      ' Notify the user. 
       MsgBox "Cannot Delete the Record." 
    End If 
End Sub