Results 1 to 9 of 9
  1. #1
    orcinus is offline Advanced Beginner
    Windows XP Access 2003
    Join Date
    Apr 2010
    Posts
    67

    Evaluating Null in a Case Function

    I have a case function which I want to return values in a query that reads a language type. Some of the language records will be null and for those, I want "BLANK" returned. When I use the below code, I don't get anything returned in the query field. All the other non null values appear without issue.

    Any Suggestions? The null evaluation is at the end of the function.

    Thanks!
    Function fcnConvLang(Lang As Variant) As String


    Select Case Lang
    Case "CANTONESE"
    fcnConvLang = "CHINESE"
    Case "MANDARIN"
    fcnConvLang = "CHINESE"
    Case "ENGLISH"
    fcnConvLang = "OTHER"
    Case "JAPANESE,NIHONGO"
    fcnConvLang = "OTHER"
    Case Null
    fcnConvLang = "BLANK"
    End Select
    End Function

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    The field may contain a zero length string, so try

    Case ""
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    orcinus is offline Advanced Beginner
    Windows XP Access 2003
    Join Date
    Apr 2010
    Posts
    67
    The field is not a zero length string (I checked with what you suggested - no dice - I also made a query where the field criteria is null and it gives records)..

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Hmm. Is it practical to let those get caught with an Else or are you using that for something else?
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    orcinus is offline Advanced Beginner
    Windows XP Access 2003
    Join Date
    Apr 2010
    Posts
    67
    I can use the Case Else and that does work. Curious though why 'Case Null' doesn't work? btw, also tried Case Is Null and Case IsNull(Lang). Neither of these worked.

  6. #6
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    I was surprised it didn't work too. If I get time I'll play with it, but at least you have something that works.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  7. #7
    orcinus is offline Advanced Beginner
    Windows XP Access 2003
    Join Date
    Apr 2010
    Posts
    67
    Cool..thanks so much for your help!

  8. #8
    ssanfu is offline Master of Nothing
    Windows 2K Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Hi Paul & orcinus

    I was curious also, so I tried to get the code to using NULL. No Luck. I found this in Help:
    Expressions that you might expect to evaluate to True under some circumstances, such as If Var = Null and If Var <> Null, are always False. This is because any expression containing a Null is itself Null and, therefore, False.
    So, even in the Select Case, comparing Null (in lang) to Case NULL will result in NULL (False).

    I did come up with a couple of work arounds:

    1) Test for NULL before the case statements:
    Code:
    Function fcnConvLang(Lang As Variant) As String
       If IsNull(Lang) Then
          fcnConvLang = "BLANK"
       End If
       
       Select Case Lang
          Case "CANTONESE"
             fcnConvLang = "CHINESE"
          Case "MANDARIN"
             fcnConvLang = "CHINESE"
          Case "ENGLISH"
             fcnConvLang = "OTHER"
          Case "JAPANESE,NIHONGO"
             fcnConvLang = "OTHER"
       End Select
       
    End Function


    2) Check for NULL in the calling statement and changing "Lang" from a Variant to a String:

    txtReturn = fcnConvLang(nz(txtEntry, "Is_Null"))
    Code:
    Function fcnConvLang(Lang As String) As String
       
       Select Case Lang
          Case "CANTONESE"
             fcnConvLang = "CHINESE"
          Case "MANDARIN"
             fcnConvLang = "CHINESE"
          Case "ENGLISH"
             fcnConvLang = "OTHER"
          Case "JAPANESE,NIHONGO"
             fcnConvLang = "OTHER"
          Case "Is_Null"
             fcnConvLang = "BLANK"
          Case Else
             fcnConvLang = "BLANK"
       End Select
       
    End Function

  9. #9
    orcinus is offline Advanced Beginner
    Windows XP Access 2003
    Join Date
    Apr 2010
    Posts
    67
    Thanks, those "workarounds" are useful. Much appreciated..

    Quote Originally Posted by ssanfu View Post
    Hi Paul & orcinus

    I was curious also, so I tried to get the code to using NULL. No Luck. I found this in Help:

    So, even in the Select Case, comparing Null (in lang) to Case NULL will result in NULL (False).

    I did come up with a couple of work arounds:

    1) Test for NULL before the case statements:
    Code:
    Function fcnConvLang(Lang As Variant) As String
      If IsNull(Lang) Then
          fcnConvLang = "BLANK"
       End If
       
       Select Case Lang
          Case "CANTONESE"
             fcnConvLang = "CHINESE"
          Case "MANDARIN"
             fcnConvLang = "CHINESE"
          Case "ENGLISH"
             fcnConvLang = "OTHER"
          Case "JAPANESE,NIHONGO"
             fcnConvLang = "OTHER"
       End Select
       
    End Function


    2) Check for NULL in the calling statement and changing "Lang" from a Variant to a String:

    txtReturn = fcnConvLang(nz(txtEntry, "Is_Null"))
    Code:
    Function fcnConvLang(Lang As String) As String
       
       Select Case Lang
          Case "CANTONESE"
             fcnConvLang = "CHINESE"
          Case "MANDARIN"
             fcnConvLang = "CHINESE"
          Case "ENGLISH"
             fcnConvLang = "OTHER"
          Case "JAPANESE,NIHONGO"
             fcnConvLang = "OTHER"
          Case "Is_Null"
             fcnConvLang = "BLANK"
          Case Else
             fcnConvLang = "BLANK"
       End Select
       
    End Function

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Combo is Null Function Help
    By fabian1675 in forum Forms
    Replies: 1
    Last Post: 02-10-2012, 05:57 PM
  2. DLookUp function giving invalid use of null error
    By shubhamgandhi in forum Programming
    Replies: 4
    Last Post: 07-21-2011, 06:04 PM
  3. Replies: 3
    Last Post: 05-06-2011, 02:49 PM
  4. Replies: 2
    Last Post: 01-03-2011, 05:17 PM
  5. Evaluating Math Strings
    By Rawb in forum Programming
    Replies: 18
    Last Post: 09-01-2010, 07:50 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums