Results 1 to 4 of 4
  1. #1
    VAer's Avatar
    VAer is offline Competent Performer
    Windows 10 Access 2013 32bit
    Join Date
    Sep 2016
    Location
    USA
    Posts
    163

    Can I use WHERE and ORDER BY together?

    SELECT * FROM Customers WHERE CustomerID < 10000


    ORDERBY Country ASC, CustomerName DESC;

    Is it a valid way to have
    WHERE and ORDER BY in the same line of code?

    Thanks.

  2. #2
    orange's Avatar
    orange is offline Moderator
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,850
    Need a space between Order BY
    See this for info


    Here is a sample from one of my test queries.

    Code:
    SELECT tblEquipInOut.TransactionId
    , tblEquipInOut.TransactionDate
    , tblEquipInOut.CrewChiefName
    , tblEquipInOut.EquipOutIn
    , tblEquipInOut.BarCode
    FROM tblEquipInOut
    WHERE (((tblEquipInOut.EquipOutIn)="IN"))
    ORDER BY tblEquipInOut.TransactionDate;

  3. #3
    VAer's Avatar
    VAer is offline Competent Performer
    Windows 10 Access 2013 32bit
    Join Date
    Sep 2016
    Location
    USA
    Posts
    163
    Quote Originally Posted by orange View Post
    Need a space between Order BY
    See this for info
    Thanks. That was just a typo.

  4. #4
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,845
    Is it a valid way to have WHERE and ORDER BY in the same line of code?
    Just out of curiosity, why don't you just try it?

    The answer is it depends. In a query you can write

    SELECT * FROM Customers WHERE CustomerID < 10000 ORDER BY Country ASC, CustomerName DESC;

    or pretty much any combination - words need to be separated by one or more spaces or a return

    Code:
    SELECT
     *
     FROM
     Customers
     WHERE
             CustomerID
     <
    
    10000
                              ORDER BY
     Country
     ASC
    ,
     CustomerName
     DESC
    
    
    would also work

    Normal convention is to break it up into the different sections
    SELECT
    FROM
    WHERE
    GROUP BY
    HAVING
    ORDER BY

    but if writing the query in vba, you are creating a string - so you need to make sure you have spaces between words - or I guess use vbcrlf - though I have never tried it that way.



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

Similar Threads

  1. Replies: 2
    Last Post: 03-07-2016, 05:02 PM
  2. Replies: 3
    Last Post: 02-09-2016, 04:36 PM
  3. Replies: 23
    Last Post: 09-14-2015, 01:34 PM
  4. Allow more than order in an ORDER form.
    By kiko in forum Access
    Replies: 37
    Last Post: 04-19-2013, 05:30 AM
  5. How to order columns in ascending order?
    By darkingthereturn in forum Access
    Replies: 5
    Last Post: 06-18-2012, 05: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