Results 1 to 4 of 4
  1. #1
    Bcanfield83 is offline Advanced Beginner
    Windows 7 32bit Access 2016
    Join Date
    May 2018
    Posts
    81

    Question Using IIF Statement In SQL String (To Oracle Database Via ODBC)

    Hi,
    I'm attempting to query an Oracle Database through a VBA module in my Access DB. Within the query, I'm trying to use CASE WHEN..THEN..ELSE to evaluate a particular column.
    If I login to this Oracle Database using TOAD, the "CASE WHEN.." syntax shown below works perfectly.

    Code:
    SELECT SG_DESCRIPTION, CON_DESCRIPTION, 
    CASE WHEN ASI_AA_LEVEL = 'E' THEN 'Enabled' ELSE 'Disabled/Nd Open Clt' END as SCREEN_STATUS
    FROM MYTABLE
    However, I'm having trouble translating that into my VBA module. I keep receiving a "Missing Expression" error when trying to run the querydef, so I must be missing something in my syntax but can't seem to figure out where I'm going wrong, e.g.:

    Code:
    strSQL = "SELECT SG_DESCRIPTION, CON_DESCRIPTION," & _
    " CASE WHEN ASI_AA_LEVEL = 'E' THEN 'Enabled' ELSE 'Disabled/Nd Open Clt' END as SCREEN_STATUS," & _
    " FROM MYTABLE"
    Within the VBA module, I have the SQL Query defined in a string variable and then creating a querydef with a connection to an Oracle Database (the connection string is defined under strConnect)

    Code:
    Dim db As DAO.Database
    Dim qdef As DAO.QueryDef
    Dim strSQL As String
    Dim strConnect As String
    
    strConnect = "<Connection String for ODBC>"
    
    Set db = CurrentDb
    
    Set qdef = db.CreateQueryDef("MyQuery")
     
    With qdef
     
            .Connect = strConnect
            .SQL = strSQL
            .ReturnsRecords = True
           
    End With


  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
    You've got a stray comma after the last field:

    " CASE WHEN ASI_AA_LEVEL = 'E' THEN 'Enabled' ELSE 'Disabled/Nd Open Clt' END as SCREEN_STATUS," & _
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    Bcanfield83 is offline Advanced Beginner
    Windows 7 32bit Access 2016
    Join Date
    May 2018
    Posts
    81
    Quote Originally Posted by pbaldy View Post
    You've got a stray comma after the last field:

    " CASE WHEN ASI_AA_LEVEL = 'E' THEN 'Enabled' ELSE 'Disabled/Nd Open Clt' END as SCREEN_STATUS," & _
    Thanks, that was actually a syntax error from me attempting to copy and paste the string into my post - oops!
    I did figure this out though - for some reason, when I build the SQL string in Access, it only works if I enclose the "CASE WHEN..ELSE..END" portion in parenthesis like so:

    Code:
    (CASE WHEN ASI_AA_LEVEL = 'E' THEN 'Enabled' ELSE 'Disabled/Nd Open Clt' END) as ACCESS_LEVEL
    What threw me off is that it works fine without the parenthesis in TOAD for Oracle:

    Code:
    CASE WHEN ASI_AA_LEVEL = 'E' THEN 'Enabled' ELSE 'Disabled/Nd Open Clt' END as ACCESS_LEVEL

  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
    Glad you got it sorted. I'm a bit surprised, as pass through queries are executed on the server, so the same syntax should work. That said, I have no Oracle experience, only SQL Server.
    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: 09-16-2018, 06:26 AM
  2. Replies: 1
    Last Post: 03-02-2015, 09:44 AM
  3. ODBC with Oracle
    By asearle in forum Queries
    Replies: 1
    Last Post: 09-07-2011, 10:22 PM
  4. Oracle ODBC Problems
    By dhall@vermeer.com in forum Access
    Replies: 1
    Last Post: 09-08-2010, 10:01 AM
  5. ODBC -- call failed. | Oracle ODBC
    By drdexter33 in forum Access
    Replies: 1
    Last Post: 04-03-2010, 09:32 PM

Tags for this Thread

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