IF Then test failing

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello Again,

I'm back. If Microsoft charged for the use of this forum they would be rich
off of me alone.

Anyway, here's another scenario I find myself in again.

I have this line of code in the AfterUpdate event of a button named
"btnAdd". The code is supposed to check if a selection has been made in a
Combo Box named "cboEmp_Name". If no selection was made it's supposed to exit
the Sub routine. Well, it ain't workin'.

If Me.cboEmp_Name.Value = Null Then
MsgBox "Please enter Employee Name.", vbExclamation, "Can't add records"
Exit Sub
End If

When I use:
Debug.Print Me.cboEmp_Name.Value

It displays the correct value of cboEmp_Name, but when the code runs the IF
test fails and the underlying code executes anyway. Does anyone know why this
happens?

-Simon
 
Simon try this....

If Not Me.cboEmp_Name.Value = "" Then
MsgBox "Please enter Employee Name.", vbExclamation, "Can't add records"
Exit Sub
End If


I'm no expert and there may be a valid reason not to do what I have
suggested, but my rational is this: I am assuming cboEmp_Name is a text box
and will probably be blank as opposed to Null.

HTH.
 
Hi Simon,

Null is an *unknown* value, therefore, if can not evaluated using =, <, >
etc. You can however test for null using the IsNull() function. Try your if
statement this way:

If IsNull(Me.cboEmp_Name) Then
MsgBox "Please enter Employee Name.", vbExclamation, "Can't add records"
Exit Sub
End If

HTH
Steve C
 
Generally the clause x = Null does not work.
You have to use IsNull(x).
Expressions involving Null don't obey the simple comparision relations.
 
Guys,

Thanks to all of you for responding, especially so quickly. Man, Access just
seems to be full of surprises. Anyway, the test is working very well now
using the "IsNull" test.

If not for all of you guys out there my families feelings would probably be
injured by now. So, from my heart and the hearts of my family members we
thank all of you very much :-)

-Simon
 
User,

I didn't know about the IsEmpty() thing. I must look that one up in the help
and see how it works.

Thanks for passing that one along :-)

-Simon
 
User said:
Just to add to the fun, in visual basic there is also that "IsEmpty()"
thing.

But that wouldn't work in this case, as it just has to do with whether a
variable has been initialized.
 
Back
Top