Results 1 to 5 of 5
  1. #1
    ohmydatabase's Avatar
    ohmydatabase is offline Advanced Beginner
    Windows 7 64bit Access 2016
    Join Date
    Feb 2017
    Posts
    79

    SQL: Problems combing UNION SELECT & ORDER BY (with logic)

    Hello,

    I am running into issues combing the UNION SELECT and ORDER BY commands in Access SQL. My two select statements select from the same table and both utilize SELECT *. The WHERE conditions on each of these SELECT's are slightly different and call for two separate queries. I then use one of the fields from the SELECT statement to customize the ordering of the query.

    Code:
    SELECT * 
     FROM [tblStatus] 
      WHERE Program = 'ABC' 
      AND (StartDate BETWEEN #8/29/2017# AND #8/31/2017# OR Ongoing = True) 
      AND 
       (
        OGAuthor IN 
         (
          SELECT LeadName 
           FROM tblLead 
            WHERE tblLead.Program = 'ABC' 
            And Department = 'SW'
         ) 
        OR OGAuthor IN 
         (
          SELECT FullName 
           FROM tblUsers 
            WHERE tblUsers.Department = 'SW'
         )
       ) 
      AND Urgent = True 
      AND isCurrent = True 
    UNION SELECT * 
     FROM [tblStatus] 
      WHERE OGAuthor = 'John Smith' 
      AND (StartDate BETWEEN #8/29/2017# AND #8/31/2017# OR Ongoing = True) 
      AND Urgent = True 
      AND isCurrent = True 
    ORDER BY IIf([Program] Like '*General', 1, 0) DESC, Program, SubProgram, Task, Initials DESC, StartDate DESC, OGStartDate DESC, ID ASC;
    I believe the problem is coming from the bit of logic in my ORDER BY clause. If I remove the "IIf" logic from the ORDER BY statement, it works. However, I really want to use that bit because it's better (to me) to use that instead of introducing an entirely new field just for ordering based on the likeliness to "*General" Currently, I get the following error.

    Runtime Error: 3351
    The ORDER BY expression (IIf([Program] Like '*General',1,0))) includes fields that are not selected by the query. Only those fields requested in the first query can be included in a ORDER BY expression.

    This is strange to me because 'Program' is included in both queries. So this error is messing me up.



    Additional Articles:
    https://www.pcreview.co.uk/threads/u...-work.1165529/
    https://www.accessforums.net/showthr...723#post368723

  2. #2
    PLangley is offline Novice
    Windows 7 64bit Access 2013 32bit
    Join Date
    Sep 2017
    Location
    Franklin, VA, USA
    Posts
    7
    You need to create a column in your both queries whose value is
    (IIf([Program] Like '*General', 1, 0)) As SortP

    Then use the SortP to order by.

    Example (from one of my databases):

    SELECT Equipment.Equipment, (IIf([Equipment] Like "*Line*",1,0)) AS SortP
    FROM Equipment
    UNION ALL SELECT Equipment.Equipment, (IIf([Equipment] Like "*Cup*",2,0)) AS SortP
    FROM Equipment
    ORDER BY SortP DESC;

    Regards,
    Pat Langley

  3. #3
    ohmydatabase's Avatar
    ohmydatabase is offline Advanced Beginner
    Windows 7 64bit Access 2016
    Join Date
    Feb 2017
    Posts
    79
    I was having issues with that previously. What I was noticing was that the 'Like' function in VBA didn't go well when defining a new Field SortP. This could have something to do with the use of SELECT *, but I tried something similar and it didn't see to like it. But I'll give this method another go. Thank you.

  4. #4
    PLangley is offline Novice
    Windows 7 64bit Access 2013 32bit
    Join Date
    Sep 2017
    Location
    Franklin, VA, USA
    Posts
    7
    Be sure to enclose the entire formula (IIF..) in parenthesis when setting up the column, as I did in the example. Watch the use of single-quotes vs double-quotes. In VBA, use double-quotes except when putting quotes around strings inside of strings. Example: strSQL = "SELECT * FROM MyTable WHERE x = 'my value' ORDER BY x". Notice the use of the single quotes around the string value.

    Hope that helps.
    Pat Langley

    (sheeee, I can't seem to say this right today. edited 3 times)

  5. #5
    ohmydatabase's Avatar
    ohmydatabase is offline Advanced Beginner
    Windows 7 64bit Access 2016
    Join Date
    Feb 2017
    Posts
    79
    Thanks for the help!

    I got around this problem by getting rid of the UNION SELECT clause. Im happy it worked, but a little let down as to why I couldn't get the IIf logic to work. Another oddity I noticed about the UNION SELECT function is that it will only query strings to 255 characters. This problem was also avoided by getting rid of the UNION SELECT clause.

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

Similar Threads

  1. Union Query Order By Specific Row Values
    By Dormie in forum Queries
    Replies: 3
    Last Post: 02-18-2015, 10:42 AM
  2. Replies: 3
    Last Post: 03-06-2014, 02:41 PM
  3. VBA for SELECT CASE logic
    By rlsublime in forum Programming
    Replies: 1
    Last Post: 12-04-2012, 09:09 PM
  4. Purchase Order Preview Problems
    By jordanturner in forum Forms
    Replies: 5
    Last Post: 10-03-2010, 10:10 AM
  5. Logic statement to select report
    By AKQTS in forum Reports
    Replies: 7
    Last Post: 07-30-2010, 12:52 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