views:

60

answers:

3

I have a text column in one of my MS Access tables that is empty be default when a new record is inserted. The problem I have is that I can't seem to check whether this field is empty with EITHER equals Null or equals "" . For example, neither of these "reads" the field as being empty:

If [Field] = "" Or [Field] = Null Then

I've always used Access 2007 to develop this database, and I recently opened it with Access 2003, which is when I think this problem started. Could that be the cause? If so, would simply opening and saving it again with Access 2007 solve it?

+2  A: 

When you compare against null, you need to use the IsNull function. With traditional ANSI SQL logic, Null <> Null, so you need a special function to test for null.

If [Field] = "" Or IsNull([Field])
Tmdean
Thank you very much!
jorrit787
Remou
+2  A: 

First off, I would suggest that you do one of two things:

  1. set your fields to disallow zero-length strings. That way you'd have to test only Is Null.

  2. if you feel you must allow storage of ZLS, then set the default value to a ZLS. It's still possible for the field to end up Null, though, so this may or may not be helpful.

I don't see storage of ZLS's as having any utility whatsoever. It's almost always a shortcut to accomodate data being appended from sources that return ZLS's instead of Nulls for empty fields.

You should also read up on Nulls. Allen Browne has a number of pages that explain it all quite well:

Aspects of working with Nulls in VBA code:

The articles are Access-oriented, but could be valuable to those using any database, particularly relative novices because of the conversational style of the writing.

David-W-Fenton
A: 

You should try:

If Nz([Field], "") = "" Then 

Because:

If [Field] = ""

in:

If [Field] = "" Or IsNull([Field])

Would still throw a Null Error!

DarkSprout
Simply not true. Null = "" returns Null, which is Not True, and evaluates correctly in the Boolean test, which when the field is Null evalutes to If Null Or True Then. Since (Null Or True) evaluates to True, there is no problem here at all. -1 for not testing before posting.
David-W-Fenton