Full Version: Why doesn't my simple conditional work
UtterAccess Forums > Microsoft® Access > Access Forms
helloro
If Me.txtMonkey = "" Then
blah blah blah
End If
Why isn't the conditional triggered (I never get inside the If statement), even though txtMonkey is blank?
Am I using the wrong thing to signify a "blank"? (nothing is entered in txtMonkey).
JimCagle
Try:
If IsNull(Me.txtMonkey) then
blah blah blah
End If
helloro
that works, thank you.
JimCagle
Sweet!!!!
Good Luck...
DougY
You may want to capture both Nulls and blanks...
If Len(NZ(Me.txtMonkey,"") = 0 Then
Blah Blah Blah
End If
HTH
datAdrenaline
I hate to jump in on a completely answered post ... but here is a trick the UA member Doctor9 exposed me to that I find quite usefull ...

If Me.txtMonkey & "" = "" Then
Blah Blah Blag
End if

This little trick works because if you concatenate a ZLS (Zero Length String) to a Null you will get a ZLS .... Also, IF, by chance the text box is a ZLS ... A ZLS concatenated with a ZLS is a ZLS. One negative to this approach is that some coding guru's and pseudo standards may indicate to not check for equivalency to a ZLS. If you subsribe to that coding style, then I would suggest ...

If Len(Me.txtMonkey & "") = 0 Then
Blah Blah Blag
End if



Edited by: datAdrenaline on Sun Dec 18 23:49:29 EST 2005.
JimCagle
There's always a better way to skin the Access cat, right???
DougY
Don't you just love that we can come up with different (and useful, if I may add) solutions to a question?? o!
One question... three solutions... this site rocks uarulez2.gif
Edit: Jim you are right ... frown.gif
datAdrenaline
I think this just proves one thing ...
He all "peg the geek meter"! ...
compute.gif
DougY
Yes we do... this site is full of geeks... geeks rule !!!
JimCagle
Embrace the Geekiness.
niesz
Yet another option...
If Nz(Me.txtMonkey) = "" then ....
ChrisO
I think for speed: -
CODE
If Len(Me.txtMonkey) Then
Else
    'blah blah blah
End If
Requires testing for Geekiness. (But think two to three times faster.)
iandouglas
Another question 'cos I'm learning always learning............
hy
If Len(NZ(Me.txtMonkey,"") = 0 Then....
rather than just
If Len([Me.txtMonkey]) = 0 Then.....
freakazeud
Also check this very very similar: earlier discussion frown.gif!
MrSiezen
That will prevent you from getting an Invalid use of Null error.
If Me.txtMonkey] contains a Null value, you'll get Null as result.
DougY
Null doesn't have a length... you will raise an error...
TH
ChrisO
CODE
Sub Test()
    Dim vntV As Variant
    
    [color="green"]'   When Null, Len() returns Null.[/color]
    vntV = Null
    If Len(vntV) Then
        [color="green"]'[/color]
    Else
        MsgBox "Len(vntV) is regarded as false but is not equal to False"
    End If


    [color="green"]'   When a ZLS, Len() returns zero.[/color]
    vntV = ""
    If Len(vntV) Then
        [color="green"]'[/color]
    Else
        MsgBox "Len(vntV) is equal to False"
    End If


    [color="green"]'   When there is at least one character, Len() returns > zero.[/color]
    vntV = "1"
    If Len(vntV) Then
        MsgBox "Len(vntV) is regarded as true but is not equal to True"
    Else
        [color="green"]'[/color]
    End If

End Sub

en(Null) does not raise an error but returns Null.
If we regard Null as the unknown then the length of Null is also unknown.
The If function must return True XOR False but since Null can not equate to True the If function must return False.
Sorry if this is starting to sound like 'Geek Speak Week' but that seems to be the way it is. wary.gif
Regards,
Chris.
DougY
Hi Chris,
irst . . .
<
Please don't be (at least not on my account)... the POV exchange (AKA Geek Speak) widen one's knowledge and understanding...one of the things I love about UA.
Now...you are correct... Len of Null will return False by the fact that it is not True. My reply was inaccurate (I should stop replying in a hurry before going to work).
HAs an addendum to the subject -- trying to use Len(NullValue) (as in: MsgBox txtValue -- Where txtValue is null) will raise an error. This and wanting to value the results of the Len() are reasons I NZ a null (new geek term...NZ is now a verb frown.gif )
Thank you for catching it and expending on the subject.
Best wishes,
ChrisO
No problems, but everything needs to be tested, even the way we test it.
MsgBox requires a string not a Null. A MsgBox requires a statement of fact not an unknown.
If presented with a Null the MsgBox could say shrug.gif , but we will have to leave that up to Micro$oft to change.
The point I’m trying to raise here is that we read things that are stated as fact. Some of those things emanate out of Micro$oft or, indirectly, from some other source.
We are entitled to believe our assumptions but are not entitled to replace fact with our assumptions. Such replacement requires proof.
Somewhat obscure but further reading.
Hope that helps and doesn’t muddy the waters.
Regards,
Chris.
This is a "lo-fi" version of UA. To view the full version with more information, formatting and images, please click here.