Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsFormsForms ProgrammingQueriesModules / DAO / VBAReports / PrintingMacrosDatabase DesignSecurityConversionImporting / LinkingSQL Server / ADPMultiuser / NetworkingReplicationSetup / ConfigurationDeveloper ToolkitsActiveX ControlsNew UsersGeneral 1General 2
Access DirectoryToolsTutorialsUser Groups
Related Topics
SQL ServerOther DB ProductsMS OfficeMore Topics ...

MS Access Forum / Modules / DAO / VBA / April 2008

Tip: Looking for answers? Try searching our database.

Is Not Null

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Mike P - 11 Apr 2008 19:58 GMT
I am running a query that should only produce records in which FieldX Is Not
Null.  However, records in which FieldX’s look Null are showing up in the
query.

In my module which populates the table with FieldX, I have a variable that
is set to
strFieldX = “”
strFieldX may or may not get string data appended to it while processing. At
the end of my processing loop, I have
Rec.FieldX = strFieldX

Is it possible that “” is equal to something other than Null?   If so, any
ideas on how to make a blank strFieldX load the value of Null in rcd.FieldX
when strFieldX = “”
Otherwise, I keep getting records that appear to be Null showing up with I
use the criteria of Is Not Null.

Thanks in advance for your help.
John Spencer - 11 Apr 2008 20:07 GMT
You are correct.  "" is not null it is a zero-length string.

You can use criteria

Field: Whatever
Criteria: is not Null and <> ""

or change your procedure to

If strFieldX <> "" Then Rec.FieldX = strFieldX

Or if you really want to be sure that Rec.Fieldx is null

IF strFieldX = "" Then
 Rec.FieldX = NULL
ELSE
 Rec.FieldX = strFieldX
END IF

John Spencer
Access MVP 2002-2005, 2007-2008
Center for Health Program Development and Management
University of Maryland Baltimore County

> I am running a query that should only produce records in which FieldX Is Not
> Null.  However, records in which FieldX’s look Null are showing up in the
[quoted text clipped - 14 lines]
>
> Thanks in advance for your help.
Dirk Goldgar - 11 Apr 2008 20:20 GMT
>I am running a query that should only produce records in which FieldX Is
>Not
[quoted text clipped - 10 lines]
>
> Is it possible that “” is equal to something other than Null?

Yes.  The zero-length string "" is *never* equal to Null.  Null is a "no
data" condition, "" is data that happens to have zero-length.

> If so, any
> ideas on how to make a blank strFieldX load the value of Null in
> rcd.FieldX
> when strFieldX = “”

Instead of

> Rec.FieldX = strFieldX

... how about using this:

   If Len(strFieldX) > 0 Then
       Rec.FieldX = strFieldX
   Else
       Rec.FieldX = Null
   End If

If you find yourself using this sort of logic in lots of places, you can
write a function to do it:

'----- start of code -----
Function ZLStoNull(sValue As String) As Variant

   If Len(sValue) > 0 Then
       ZLStoNull = sValue
   Else
       ZLStoNull = Null
   End If

End Function
'----- end of code -----

Then you could call that function wherever you make these assignments:

   Rec.FieldX = ZLStoNull(strFieldX)

You can run an update query to fix up those records that already have
zero-length strings where you'd rather have Null:

   UPDATE YourTableName
   SET FieldX = Null
   WHERE FieldX = "";

> Otherwise, I keep getting records that appear to be Null showing up with I
> use the criteria of Is Not Null.

I would also recomment setting the field's AllowZeroLength property (in
table design) to No.  You would do this after having run the update query
above.  Bear in mind that doing so will cause an error to be raised any time
you try to store "" in the field, so it will cause errors in existing code
that assigns "" to the field.  That's good, if your intention is henceforth
to prevent that from happening.

An alternative to this fix is to allow zero-length strings in your code, but
change your queries to filter on Null OR ZLS.  You can do that in one handy
criterion by using the concatenation operator, like this:

   WHERE FieldX & "" <> ""

However, I don't like this approach.  I prefer that if a zero-length string
is to be treated the same as Null -- which is most of the time, in my
applications -- I just don't let the ZLS get into the field in the first
place, and use only Null to represent the no-data condition.

Signature

Dirk Goldgar, MS Access MVP
www.datagnostics.com

(please reply to the newsgroup)

Brent Spaulding (datAdrenaline) - 12 Apr 2008 07:30 GMT
In addition to the excellent advice/information given, I would like to add
the following with respect to data input and the ZLS (Zero Length String)
and Null ...
------------------------------
When you enter "" (A ZLS)
- in a numeric field that IS NOT required, a Null will be stored.
- in a numeric field that IS required, you get an error.
- in a text field that allows ZLS's, a ZLS will be stored.
- in a text field that IS NOT required and DOES NOT allow ZLS's, a Null will
be stored.
- in a text field that IS required and DOES NOT allow ZLS's, you get an
error
- in an UNBOUND control, a Null is returned by the control

Signature

Brent Spaulding | datAdrenaline | Access MVP

>I am running a query that should only produce records in which FieldX Is
>Not
[quoted text clipped - 17 lines]
>
> Thanks in advance for your help.
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.