Results 1 to 9 of 9
  1. #1
    jasonm is offline Novice
    Windows Vista Access 2003
    Join Date
    Oct 2012
    Posts
    22

    SQL Statement using WHERE with CASE and CHECKBOX

    I have a sql statement in which I am using the CASE expression in the WHERE clause based on whether a checkbox is selected or not.

    Basically if the checkbox (Check59) is selected, I want the WHERE clause to include Security Level 3 (If the checkbox is NOT selected, only Security Level 1 and 2 shoudl be included.

    Here is my code:



    SELECT DISTINCTROW BEmployees.EmployeeCRN AS Expr1, BEmployees.LastName AS Expr2, BEmployees.FirstName AS Expr3, [FirstName] & " " & [LastName] AS Consultant
    FROM BEmployees INNER JOIN [Name Substitution] ON BEmployees.FirstName = [Name Substitution].NameToShow
    WHERE
    (([Name Substitution].Current_User)=CurrentUser()) AND
    CASE WHEN Me.Check59=1 Then (((BEmployees.SecurityLevel)=1 Or (BEmployees.SecurityLevel)=2 Or (BEmployees.SecurityLevel)=3)
    ELSE (((BEmployees.SecurityLevel)=1 Or (BEmployees.SecurityLevel)=2)
    END
    ORDER BY BEmployees.LastName, BEmployees.FirstName DESC , [FirstName] & " " & [LastName];

    BTW I am a newbie.
    THX

  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,632
    What is the issue? What happens when you open that query?
    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
    jasonm is offline Novice
    Windows Vista Access 2003
    Join Date
    Oct 2012
    Posts
    22
    The error is:

    Syntax error (missing operator) in query expression..........and it proceeds to list the code between the WHERE and END??

  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,632
    AFAIK, CASE does not work in Access.
    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
    jasonm is offline Novice
    Windows Vista Access 2003
    Join Date
    Oct 2012
    Posts
    22
    ??? I have other CASE situations in other places in the DB???

  6. #6
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,632
    In other queries?

    Select Case is valid structure in VBA but I don't think Access query engine can handle it.
    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.

  7. #7
    jasonm is offline Novice
    Windows Vista Access 2003
    Join Date
    Oct 2012
    Posts
    22
    I think you are right - sorry I am trying to get up to speed.

    I have this in a Row Source of a Form.

    Is there any other way I can accomplish this?

  8. #8
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,632
    Do you mean Record Source?

    I don't use dynamic parameterized queries. Review: http://www.allenbrowne.com/ser-62.html
    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.

  9. #9
    CJ_London is offline VIP
    Windows 8 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,842
    use switch rather than case in sql, beats a load of nested iif's, however for here an iif will do (assuming using access backend)

    Code:
    SELECT DISTINCTROW BEmployees.EmployeeCRN AS Expr1, BEmployees.LastName AS Expr2, BEmployees.FirstName AS Expr3, [FirstName] & " " & [LastName] AS Consultant
     FROM BEmployees INNER JOIN [Name Substitution] ON BEmployees.FirstName = [Name Substitution].NameToShow
     WHERE
     (([Name Substitution].Current_User)=CurrentUser()) AND 
    iif(Me.Check59=true,BEmployees.SecurityLevel=1 Or BEmployees.SecurityLevel=2 Or BEmployees.SecurityLevel=3, BEmployees.SecurityLevel=1 Or BEmployees.SecurityLevel=2)=True
    ORDER BY BEmployees.LastName, BEmployees.FirstName DESC , [FirstName] & " " & [LastName];
    No comment on
    whether you are referencing Check59 correctly
    why you are aliasing what looks like perfectly acceptable names with Expr1 etc
    why you are ordering desc then asc

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

Similar Threads

  1. CASE STATEMENT or IF-ELSE-THEN? HELP
    By Shakenaw in forum Access
    Replies: 9
    Last Post: 06-08-2015, 11:24 AM
  2. Case Statement
    By RussH in forum Programming
    Replies: 1
    Last Post: 08-12-2013, 02:50 PM
  3. using two criteria in one case statement
    By chessico in forum Access
    Replies: 5
    Last Post: 03-14-2012, 03:25 PM
  4. if or case statement and how
    By Dannat in forum Queries
    Replies: 1
    Last Post: 01-31-2012, 10:35 AM
  5. Case Statement Syntax
    By allenjasonbrown@gmail.com in forum Programming
    Replies: 1
    Last Post: 11-16-2010, 07:18 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