Validation Dummy

N

NotGood@All

I have this code in the before update event for a textbox of a customer's
first name. My thinking is that if I tab past the first name I should get my
MsgBox. That does not happen. Could someone tell me if I'm thinking
incorrectly or if this code is incorrect. Thank You Very Much!!!

Private Sub Customer_s_First_Name_BeforeUpdate(Cancel As Integer)
If Me.Customer_s_First_Name.Text = vbNullString Then
MsgBox "Value Required", vbOKOnly, "First Name Missing!!"
Cancel = True
End Sub
 
J

Jeanette Cunningham

Hi,
to test if a control is empty, use the IsNull function.
Note the IsNull function is very different from using = vbNullString.
Leave out the .text after the name of the control

Use IsNull like this:
IsNull(Me.NameOfControl)

So we get:
If IsNull(Me.Customer_s_First_Name) Then
MsgBox "Value Required", vbOKOnly, "First Name Missing!!"
Cancel = True
End If

You can also use
If Len(Me.Customer_s_First_Name & vbNullString) = 0 Then

MsgBox "Value Required", vbOKOnly, "First Name Missing!!"
Cancel = True
End If

There is a second problem with this code - if the user never goes into the
control called
Customer_s_First_Name, then the code never fires.
It is better to put this code into the BeforeUpdate event for the form.
Click on the form up near the top left where there is a square, when clicked
it is highlighted.
Check on the property sheet that you have the BeforeUpdate event for the
form and not the control, then add the validation code.


When you want to empty a control you can usually use
Me.NameOfControl = Null


Jeanette Cunningham MS Access MVP -- Melbourne Victoria Australia
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top