Problems validating data in forms

  • Thread starter Thread starter Edgar Cohen
  • Start date Start date
E

Edgar Cohen

I am new to the VBA world. I have a form that I am trying to validate
the fields on. They are either text boxes or combo boxes. I have a
custom function that is called when the user clicks a command button. I
cannot test any of these fields for no user input or null. None of these
work:

If (Me.txtProductCode = Null) Then
' some code here
End if

or

If Len (Me.txtProductCode) = Null Then
' some code here
End if
or

If Len(Me.txtProductCode) = 0 Then
' some code here
End if

If IsNull(Me.txtProductCode) = True Then
' some code here
End if

I have tried to use the expression builder but it is very confusing for
me. I'd rather do this in VB code. So, how do you check the contents of
form fields in VB? Where is there some very good examples of this? Thanks

(e-mail address removed)
 
Edgar said:
I am new to the VBA world. I have a form that I am trying to validate
the fields on. They are either text boxes or combo boxes. I have a
custom function that is called when the user clicks a command button.
I cannot test any of these fields for no user input or null. None of
these work:

If (Me.txtProductCode = Null) Then
' some code here
End if

or

If Len (Me.txtProductCode) = Null Then
' some code here
End if
or

If Len(Me.txtProductCode) = 0 Then
' some code here
End if

If IsNull(Me.txtProductCode) = True Then
' some code here
End if

This last one should work. Nothing is ever "equal" to null which is why the
other expressions you tried don't work. IsNull() though should do exactly what
you want (providing that an empty control is actually Null and not a zero length
string ("").
I have tried to use the expression builder but it is very confusing
for me. I'd rather do this in VB code. So, how do you check the
contents of form fields in VB? Where is there some very good examples
of this? Thanks

Something that covers both Nulls and zero length strings is a test like..

If Len(Me!ControlName & vbNullString) = 0 Then...
 
Null is, by definition, an "unknown value" that cannot be "equal" to
anything else, even the constant Null or another null value. If testing for
length, it appears you want to detect either a Null or a Null (Zero-Length)
String, so that should be

If Len(Me.txtProductCode & "") = 0 Then

If the user has entered nothing into the Control, I don't know why
If IsNull(Me.txtProductCode) = True Then
' some code here
End if

would not test True.

Larry Linson
Microsoft Access MVP
 
Back
Top