Image of Navigational Panel mapped to Contents / Home / Search Date()
Tech Support Question

Image of Line Break

Q. I have an Access 7.0 database with several Date/Time fields and, from an application which I am trying to build, I have the following code:

  If MyTable.Fields("Current_Date")="" then
      ...
  EndIf

The field is empty, but the If statement comes back as false.

I also tried Date(00,00,00) as it says in the online help, but this gives me a syntax error.

Can you please help. Thanks.

From: Gary William
Email: gwilliam@cftnet.com

A. Gary, I believe that your problem here is that you are looking at values which are Null. If you look closely at the online help (particularly for Access) you will notice that fields which have not been assigned a value are Null. Null is different to an empty or 0 value, it is a special state which indicates that the filed contains no data whatsoever, not just that the data is an empty string or a zero or whatever. For a more full explanation of Nulls the definitive source is probably 'An Introduction to Database Systems' by C. J. Date. Regardless the reason that your tests are failing is that a Null is never equal to anything. Even Null. For example the comparison 'Null = Null' will always return false.

Fortunately VB gives you a way to deal with this - the IsNull function. You can use it in code like this:

  if IsNull(rs.Fields(fieldname)) then
    MsgBox "fieldname contains a null value !"
  End if

Obviously that's a trivial example but I think you see what I mean. The trick with Nulls is generally figuring out what to do with them in your app. You will need to decide on some strategies for that yourself based upon the data requirements of your app.

Another tip: For avoiding Nulls in String fields when reading them from a recordset you can simply use the following syntax:

  txtTextBox.Text = rs.Fields(fieldname) & ""

This simply appends an empty string to each string you read in. Of course if there is a valid string value it has no effect, if the value is Null then it type casts it as a string and the result returned is an empty string.

I hope this helps, check out your VB or Access documentation on Nulls for more info.


Image of Arrow to Previous Article Image of Arrow to Next Article

[TECH SUPPORT TOC]
Image of Line Break
[HOME] [TABLE OF CONTENTS] [SEARCH]