View Full Version : Data Validation on forms BeforeUpdate event


jevans
10-21-2001, 10:02 PM
Hello Everybody,

Ok, here's my problem! I have a form (frm_Customer) that I am using to display and update customer information. I have two types of customer, personal (P) and business (B). The field Type holds either a P or B to identify customer type. I want the code in the before update event on the form to stop the update if the DisplayName (aka customer name) is null only for Type = B.

Here's my code:

Private Sub Form_BeforeUpdate (Cancel As Integer)

If Me.Type.Value = "B" And IsNull (DisplayName) = False Then
Else
MsgBox "The customer name is a required field for business customers, to continue please complete entry!", vbOKOnly
Me.DisplayName.SetFocus
Cancel = True
Exit Sub
End If

End Sub

What am I doing wrong?

Thank you in advance.

SteveA
10-22-2001, 04:07 AM
The code appears to say that everything is OK if the customer type is B and a name is entered, however this means every other combination is invalid. Try the following code:

If Me.Type.Value = "B" And IsNull(DisplayName) Then
MsgBox "The customer name is a required field for business customers, to continue please complete entry!", vbOKOnly
Me.DisplayName.SetFocus
Cancel = True
Exit Sub
End If

End Sub

HTH
SteveA