Validation Dummy

  • Thread starter Thread starter NotGood@All
  • Start date Start date
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
 
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
 
Back
Top