How to test for Null?

T

tom

I am in the process of upgrading from 97 to
03, I have a conditional that is having trouble with
a null value. It seems that the if statement
does not mind the null value but
the elseif statement is clearly raising an error
on getting a null value. There was no problem with
this code using Access 97.

So why is this and what to do about it? In C a null
value is just zero but apparently in Visual Basic a null
value is something else. Can someone please elaborate
so I can use good technique to solve this one?
 
D

Dirk Goldgar

tom said:
I am in the process of upgrading from 97 to
03, I have a conditional that is having trouble with
a null value. It seems that the if statement
does not mind the null value but
the elseif statement is clearly raising an error
on getting a null value. There was no problem with
this code using Access 97.

So why is this and what to do about it? In C a null
value is just zero but apparently in Visual Basic a null
value is something else. Can someone please elaborate
so I can use good technique to solve this one?

You don't show your code, so I can't say exactly what's wrong. I can say
that I'm not aware of any change in the handling of Null between Access
97 and 2003.

Anyway, if you want to check in VBA code to see if something is Null,
you should use the IsNull() function, like this:

If IsNull(Something) Then
' Handle the Null case.
Else
' It isn't Null.
End If

Note that a zero-length string ("") is not Null. If you need your code
to treat the two cases, Null or zero-length string, as the same, you can
use concatenation to convert Null to a ZLS:

If Len([MyField] & "") = 0 Then
' The field was either Null or a ZLS.
Else
' The field had something in it.
End If
 

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