Wrox Programmer Forums
|
Access VBA Discuss using VBA for Access programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 9th, 2004, 12:13 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,151
Thanks: 2
Thanked 14 Times in 14 Posts
Send a message via ICQ to SerranoG Send a message via AIM to SerranoG
Default Null Ignored... Why?

I have a very simple expression similar to this:
Code:
   If IsNull(Me.txtMyText) Then
      MsgBox "The textbox is null.", vbExclamation, "Error!"
   Else
       MsgBox "The textbox says " & Me.txtMyText, vbInformation, "FYI" 
   End If

However, even though Me.txtMyText is null, Access is ignoring that and evaluating the IF statement as as FALSE and failing (red line). It says it's trying to use a variable that has no value. Well, DUH, I know that; but Access is chosing to ignore the IsNull() function. This is not the first time this has happened.

I've also tried evaluating for an empty string:
Code:
   If IsNull(Me.txtMyText) or Me.txtMyText = "" Then
      MsgBox "The textbox is null.", vbExclamation, "Error!"
   Else
      MsgBox "The textbox says " & Me.txtMyText, vbInformation, "FYI"
   End If

But, again, it fails at the first line because Me.txtMyText has no value at all.

Has anyone experienced this and found a solution to the problem? Thanks.


Greg Serrano
Michigan Dept. of Environmental Quality, Air Quality Division
__________________
Greg Serrano
Michigan Dept. of Environmental Quality
Air Quality Division
 
Old January 9th, 2004, 12:22 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi there,

You can try to convert the null value to a Zero length String by appending a ZLS to it and then check for that:
Code:
If Me.txtMyText & "" = "" Then
  MsgBox "The textbox is null.", vbExclamation, "Error!"
Else
  MsgBox "The textbox says " & Me.txtMyText, vbInformation, "FYI" 
End If
Is txtMyText your text box or a variable? Shouldn't you use the Text property? Aren't you checking whether the text box itself is Null now??

Cheers,

Imar


---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
 
Old January 9th, 2004, 12:36 PM
Friend of Wrox
 
Join Date: Jul 2003
Posts: 174
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Use the following:

If len(Me.txtMyText & vbNullString) = 0 Then
     MsgBox "The textbox is null.", vbExclamation, "Error!"
Else
     vMsgBox "The textbox says " & Me.txtMyText, vbInformation, "FYI"
End If

Regards,

Beth M
 
Old January 10th, 2004, 08:55 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 120
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Greg

Another alternative:

If Nz(Me.txtMyText,"")="" Then
      MsgBox "The textbox is null.", vbExclamation, "Error!"
   Else
       MsgBox "The textbox says " & Me.txtMyText, vbInformation, "FYI"
End If


Brian Skelton
Braxis Computer Services Ltd.
 
Old January 11th, 2004, 06:56 AM
Authorized User
 
Join Date: Jul 2003
Posts: 73
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I find using ! instead . in the Me.txtMyText expression works better for the property. I leave . only for the method.


Cheers Ray
 
Old January 12th, 2004, 02:13 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,151
Thanks: 2
Thanked 14 Times in 14 Posts
Send a message via ICQ to SerranoG Send a message via AIM to SerranoG
Default

I tried the NZ() function and it failed, too, because it contained an "object that has no value," i.e. NULL. That is so odd... if it's null, the NZ() and IsNull() functions should catch it but they don't want to function as expected. I did notice that the WHERE restriction when this form opens results in zero records. Even so, those two functions should work.

I wrote a new IF statement basically saying that if the recordset is empty to not even bother to check for nulls and skip that whole section of code. That has eliminated the problem, but the question remains. Why are those two functions failing when the textbox is obviously NULL? They're supposed to test for NULL and are failing because the field is NULL! :(

BTW, I don't use the bang (!) anymore because for coding it's obsolete. When you use periods, Microsoft Access's autocomplete finishes lines of code for you. When you use the bang, it does not autocomplete.



Greg Serrano
Michigan Dept. of Environmental Quality, Air Quality Division
 
Old January 12th, 2004, 02:33 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,110
Thanks: 0
Thanked 3 Times in 3 Posts
Default

You can try reversing your logic and take action:

If Not IsNull(Me.txtMyText) THen
    MsgBox "The textbox says " & Me.txtMyText, vbInformation, "FYI"

 
Old January 12th, 2004, 06:07 PM
Authorized User
 
Join Date: Dec 2003
Posts: 81
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Have you checked the properties of the text box. I have found some weird scenarios as you describe if the properties are set funny...for example the default value set to ""

Otherwise, I have never heard of this problem and I use isnull quite often

John

 
Old January 13th, 2004, 09:11 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,151
Thanks: 2
Thanked 14 Times in 14 Posts
Send a message via ICQ to SerranoG Send a message via AIM to SerranoG
Default

I checked. There is no default value specified.

Greg Serrano
Michigan Dept. of Environmental Quality, Air Quality Division
 
Old December 5th, 2005, 10:25 AM
Registered User
 
Join Date: Dec 2005
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Did you ever find the answer to this? I recently found myself struggling with the exact same issue with a textbox on a report and couldn't figure out why IsNull wasn't working as expected. I finally found out that my textbox had the exact same name as the database field did. So in my case IsNull(SpouseName) was looking by default to the textbox named "SpouseName" and not the actual field "SpouseName" in the database. When I renamed the textbox control to "txtSpouseName", then IsNull(SpouseName) began to work correctly.







Similar Threads
Thread Thread Starter Forum Replies Last Post
Null Value Yasho VB.NET 2002/2003 Basics 1 June 21st, 2007 04:37 AM
How to set Not Null constraint to Null Columns arasu Oracle 1 August 22nd, 2005 10:09 AM
Is A Null Different from Another Null ramk_1978 SQL Language 9 February 12th, 2005 03:18 PM
when to use is Null and =null and"null shoakat Classic ASP Databases 3 October 29th, 2004 01:47 AM
Not Null morpheus SQL Server 2000 12 November 20th, 2003 05:46 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.