Results 1 to 5 of 5
  1. #1
    RunTime91 is offline Competent Performer
    Windows 8 Access 2013
    Join Date
    Dec 2014
    Posts
    284

    Trying to create a Case Statement with multiple criteria to return multiple values

    Greetings All!



    I'm close!! (I think )

    Just trying to create a Case Statement that has three criteria and returns three values.

    This works great but I can't seem to figure out how to go to the next case statement if the first value is false
    Code:
    SELECT CASE WHEN LEFT(A.SalesID, 3) ='AAA'THEN'Boo'END As Poo, IIf(Sales ='R','Rural','City') AS Boo2, IIf(Del ='S','Standard','Overnight') AS Boo3
    FROM Dbo.MTable A JOIN Dbo.Delivery B
    ON A.SalesID = B.SalesID
    WHERE B.Tracking_Number ='111-123456'
    


    The only other CASE will be when LEFT(A.SalesID, 3) = 'BBB' the other criteria will remain the same as below

    Code:
    SELECT CASE WHEN LEFT(A.SalesID, 3) ='BBB' THEN 'Boo' END As Poo, IIf(Sales ='R','Rural','City') AS Boo2, IIf(Del ='S','Standard','Overnight') AS Boo3
    FROM Dbo.MTable A JOIN Dbo.Delivery B
    ON A.SalesID = B.SalesID
    WHERE B.Tracking_Number ='111-123456'
    


    So I guess what I am trying to do is somehow combine these statements?

    And yes the codes does come from SSMS - I like to develop in SSMS then move it over to Access VBE

    Thanks for any help~







  2. #2
    pbaldy's Avatar
    pbaldy is online now Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,642
    Untested

    Code:
    CASE LEFT(A.SalesID, 3) 
      WHEN 'AAA' THEN 'Boo'
      WHEN 'BBB' THEN 'xyz'
    END
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    RunTime91 is offline Competent Performer
    Windows 8 Access 2013
    Join Date
    Dec 2014
    Posts
    284
    Hey Paul ~ Thanks for being out there

    I tried the following - I got red sqwigglys on the second WHEN Statement
    Code:
    SELECT CASE LEFT(A.SalesID, 3)
    WHEN 'AAA' THEN 'Boo' END As Poo, IIf(Sales ='R','Rural','City') AS Boo2, IIf(Del ='S','Standard','Overnight') AS Boo3
    WHEN 'BBB' THEN 'Poo' END As Boo, IIf(Sales ='R','Rural','City') AS Boo2, IIf(Del ='S','Standard','Overnight') AS Boo3
    FROM Dbo.MTable A JOIN Dbo.Delivery B
    ON A.SalesID = B.SalesID
    WHERE B.Tracking_Number ='111-123456'
    

  4. #4
    RunTime91 is offline Competent Performer
    Windows 8 Access 2013
    Join Date
    Dec 2014
    Posts
    284
    Trying a new approach

    I'm just going to use two IIf Statements (if I can)

    Here is one of the IIf Statements - What I cannot get to work is getting the SELECT Query to populate the Me.CmboUrgRou and the Me.CmboRqstType Objects
    Yes the code looks different - I'm done trying to alias it
    Code:
    If (left(Me!txtQKey, 3) = "XAW") Then
    
      StrSQL = "SELECT IIf(A.Split = 'R', 'Referral', 'Authorization') As Boo, IIf(A.UrR = 'R', 'Routine', 'Urgent') As Poo " & _
                "FROM Dbo.AQue A JOIN Dbo.Assignments B " & _
                "ON A.QKey = B.QKey " & _
                "WHERE B.EDI_Tracking_Number = '" & Me.TxtCaseRef & "'"
    
       Debug.Print StrSQL
    
    conn.Execute (StrSQL)
    
    Me.CmboCivMTF = "Civilian"
    Me.CmboRqstType = Boo
    Me.CmboUrgRou = Poo
    The Cmbo UrgRou & RqstType keep equaling NULL

    Thank you so much...

  5. #5
    pbaldy's Avatar
    pbaldy is online now Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,642
    In the first, the CASE statement was ended at "... As Poo," so the second WHEN is incomplete. I don't really understand your goal, but in the second effort I think you'd have to open a recordset on the SQL, not execute it. Then:

    Me.CmboRqstType = rs!Boo

    I don't use ADO much, so I'd probably set a pass through query to the SQL and open a DAO recordset on that query.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

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

Similar Threads

  1. Replies: 3
    Last Post: 10-30-2016, 05:50 AM
  2. select case with multiple criteria?
    By vicsaccess in forum Programming
    Replies: 2
    Last Post: 02-28-2016, 01:04 PM
  3. Replies: 5
    Last Post: 02-13-2016, 10:12 AM
  4. Replies: 4
    Last Post: 02-04-2016, 11:31 AM
  5. case statement - multiple columns
    By eddiec in forum Queries
    Replies: 2
    Last Post: 07-05-2011, 03:24 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