Results 1 to 5 of 5
  1. #1
    comfygringo is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    Jul 2013
    Posts
    12

    Switch Statement


    This code works fine without the switch statement. What am I doing wrong?

    Code:
     DoCmd.RunSQL "SELECT Salutation.Salutation, valid_numbers.[first name], valid_numbers.[last name], ([house number] & Chr(32) & " _        & "[street name]) AS address, city, st, zip, (security.[first name] & " _
            & "Switch(Len([Middle Initial]) = 0, "", Len([Middle Initial]) <> 0, Chr(32) & [Middle Initial] & '.') & Chr(32) & " _
            & "security.[last name]) AS Agent into Letters " _
            & "FROM (valid_numbers INNER JOIN salutation ON Salutation.ID = Valid_Numbers.Salutation) INNER JOIN security ON security.userid = valid_numbers.userid " _
            & "WHERE [send letter] = true and [letter sent date] is null and licensed = True and valid_numbers.userid = '" & myUserID & "'"

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,633
    I use literal space instead of Chr(32).

    Try replacing inner quote marks with apostrophes, like you did with '.'.

    "Switch(Len([Middle Initial]) = 0, '', Len([Middle Initial]) <> 0, ' ' & [Middle Initial] & '.')" & " " _

    or maybe (I never tried this but I've seen)
    "Switch(Len([Middle Initial]) = 0, """", Len([Middle Initial]) <> 0, "" "" & [Middle Initial] & ""."")" & " " _

    But with only two possible values, why use Switch?

    "IIf(Len([Middle Initial])=0, '', ' ' & [Middle Initial] & '.')"

    Why is [Middle Initial] zero length? What is actually in the field - null or empty string? I don't allow empty strings in tables.

    "IIf(IsNull([Middle Initial]),'',' ' & [Middle Initial] & '.')"
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    comfygringo is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    Jul 2013
    Posts
    12
    I got it to work. The code: & "Switch(Len([Middle Initial]) = 0, "", Len([Middle Initial]) <> 0, Chr(32) & [Middle Initial] & '.') needed to be changed to & "Switch(Len([Middle Initial]) = 0, '', Len([Middle Initial]) <> 0, Chr(32) & [Middle Initial] & '.')

    Subtle but took me forever to find.

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,633
    Did you review my complete post? I might have edited it after you posted.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,633
    My point is Switch is actually more complicated approach for this situation.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

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

Similar Threads

  1. switch statement in a access query
    By Dominic in forum Access
    Replies: 1
    Last Post: 03-27-2013, 11:32 PM
  2. Switch Statement in WHERE Clause
    By Gray in forum Queries
    Replies: 1
    Last Post: 06-02-2011, 06:50 AM
  3. Case or Switch in an update statement
    By allenjasonbrown@gmail.com in forum Queries
    Replies: 7
    Last Post: 11-17-2010, 01:49 AM
  4. Replies: 6
    Last Post: 04-06-2010, 03:00 PM
  5. Switch and Tables
    By UtilityRyan in forum Database Design
    Replies: 0
    Last Post: 06-12-2007, 03:49 PM

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