|
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
|
|
|
January 9th, 2004, 12:13 PM
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 1,151
Thanks: 2
Thanked 14 Times in 14 Posts
|
|
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
|
January 9th, 2004, 12:22 PM
|
|
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
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.
|
January 9th, 2004, 12:36 PM
|
Friend of Wrox
|
|
Join Date: Jul 2003
Posts: 174
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
January 10th, 2004, 08:55 AM
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 120
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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.
|
January 11th, 2004, 06:56 AM
|
Authorized User
|
|
Join Date: Jul 2003
Posts: 73
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
I find using ! instead . in the Me.txtMyText expression works better for the property. I leave . only for the method.
Cheers Ray
|
January 12th, 2004, 02:13 PM
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 1,151
Thanks: 2
Thanked 14 Times in 14 Posts
|
|
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
|
January 12th, 2004, 02:33 PM
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 1,110
Thanks: 0
Thanked 3 Times in 3 Posts
|
|
You can try reversing your logic and take action:
If Not IsNull(Me.txtMyText) THen
MsgBox "The textbox says " & Me.txtMyText, vbInformation, "FYI"
|
January 12th, 2004, 06:07 PM
|
Authorized User
|
|
Join Date: Dec 2003
Posts: 81
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
January 13th, 2004, 09:11 AM
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 1,151
Thanks: 2
Thanked 14 Times in 14 Posts
|
|
I checked. There is no default value specified.
Greg Serrano
Michigan Dept. of Environmental Quality, Air Quality Division
|
December 5th, 2005, 10:25 AM
|
Registered User
|
|
Join Date: Dec 2005
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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.
|
|
|