Select Case: Case Null

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi Guys,

I have a txtfield which I run thru select case logic and it works with
every possible case except Null. When the txtField is Null, it skips "Case
Null" and hits "Case Else". I checked the value in a watch window and it
definitely says Null. Any suggestions for capturing the null cases????
 
The code within the Case block will only execute if the expression evaluates
as True, and expressions involving Null values generally evaluate to neither
True nor False, but Null. The simplest solution is to test for Null using
the IsNull() function before beginning your Select Case block ...

Public Sub CaseNull(ByVal varInput As Variant)

If IsNull(varInput) Then
Debug.Print "It's Null"
Else
Select Case varInput
Case "One"
Debug.Print "It's 'One'"
Case 1
Debug.Print "It's 1"
Case Else
Debug.Print "I don't know what it is"
End Select
End If

End Sub

--
Brendan Reynolds (MVP)
http://brenreyn.blogspot.com

The spammers and script-kiddies have succeeded in making it impossible for
me to use a real e-mail address in public newsgroups. E-mail replies to
this post will be deleted without being read. Any e-mail claiming to be
from brenreyn at indigo dot ie that is not digitally signed by me with a
GlobalSign digital certificate is a forgery and should be deleted without
being read. Follow-up questions should in general be posted to the
newsgroup, but if you have a good reason to send me e-mail, you'll find
a useable e-mail address at the URL above.
 
Thanks for the info. Works Great!!!

Brendan Reynolds said:
The code within the Case block will only execute if the expression evaluates
as True, and expressions involving Null values generally evaluate to neither
True nor False, but Null. The simplest solution is to test for Null using
the IsNull() function before beginning your Select Case block ...

Public Sub CaseNull(ByVal varInput As Variant)

If IsNull(varInput) Then
Debug.Print "It's Null"
Else
Select Case varInput
Case "One"
Debug.Print "It's 'One'"
Case 1
Debug.Print "It's 1"
Case Else
Debug.Print "I don't know what it is"
End Select
End If

End Sub

--
Brendan Reynolds (MVP)
http://brenreyn.blogspot.com

The spammers and script-kiddies have succeeded in making it impossible for
me to use a real e-mail address in public newsgroups. E-mail replies to
this post will be deleted without being read. Any e-mail claiming to be
from brenreyn at indigo dot ie that is not digitally signed by me with a
GlobalSign digital certificate is a forgery and should be deleted without
being read. Follow-up questions should in general be posted to the
newsgroup, but if you have a good reason to send me e-mail, you'll find
a useable e-mail address at the URL above.
 
Back
Top