Data Validation on forms BeforeUpdate event

jevans

Registered User.
Local time
Today, 09:33
Joined
Oct 11, 2000
Messages
23
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.
 
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
 

Users who are viewing this thread

Back
Top Bottom