Results 1 to 8 of 8
  1. #1
    MatthewGrace is offline Competent Performer
    Windows 8 Access 2010 64bit
    Join Date
    Jan 2013
    Posts
    163

    Order By DateField... ASC if this / DESC if that


    My goal is to sort by a 'ShipDate' Field in my Query from oldest to newest... unless the 'ShipmentStatus' Field is marked as Shipped, in which case I want Shipped items to sort by that same Date Field but this time by newest to oldest.

    Here's a picture example if it helps:
    Click image for larger version. 

Name:	Query1.jpg 
Views:	15 
Size:	54.8 KB 
ID:	23051

    I feel like I'm overlooking something obvious. Thanks to anyone who helps.

  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,771
    AFAIK, cannot be done. A field can have only one sort parameter and not dynamic based on any criteria. Cannot build a conditional expression in Sort row of query.

    You could have subreports with different sort parameters.
    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
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,870

  4. #4
    InsuranceGuy is offline Competent Performer
    Windows 7 64bit Access 2013
    Join Date
    Aug 2015
    Location
    Colorado
    Posts
    126
    You need to intentionally generate Null values in calculated fields to accomplish this. I think this matches what you wanted to accomplish:

    Code:
    SELECT qryShipmentManager.ShipDate ,qryShipmentManager.ShipmentStatus
    FROM qryShipmentManager
    ORDER BY AlreadyShipped DESC
     ,IIf([ShipmentStatus] = "Shipped", NULL, 1) * [ShipDate] ,IIf([ShipmentStatus] = "Shipped", 1,NULL)* [ShipDate] DESC
    Cheers,

    Jeff

  5. #5
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,771
    And where do you put that IIf()? Can't do it in query builder as you have posted.
    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.

  6. #6
    InsuranceGuy is offline Competent Performer
    Windows 7 64bit Access 2013
    Join Date
    Aug 2015
    Location
    Colorado
    Posts
    126
    I'm not sure I'm following your question June. I loaded his sample data into a dummy table and built this query in the Query Builder. Feel free to give it a try. I'm very open to feedback!

    Jeff

  7. #7
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,771
    I did test but apparently had something shouldn't have because of copy/paste. Now don't get error and it does the dual sort. Still has only one sort parameter for each field as I said had to. I just don't think I would have ever figured out those calculations. Learned 2 new things today.

    And I referenced table instead of the 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.

  8. #8
    MatthewGrace is offline Competent Performer
    Windows 8 Access 2010 64bit
    Join Date
    Jan 2013
    Posts
    163
    About 20 minutes after I posted this the answer hit me like a brick... I was so excited to figure something like this out, and now I see InsuranceGuy not only beat me to it, but his answer is a little more pro looking than mine. Thanks for the help!

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

Similar Threads

  1. Replies: 9
    Last Post: 07-21-2015, 05:29 AM
  2. Sort DESC not working
    By accessnewbie352 in forum Macros
    Replies: 6
    Last Post: 01-15-2015, 02:27 PM
  3. ASC or DESC Sort Based On Another Field
    By KikoMatsing in forum Access
    Replies: 1
    Last Post: 08-12-2014, 10:34 AM
  4. Asc/Desc/reset sort button code
    By mightyteegar in forum Programming
    Replies: 2
    Last Post: 10-30-2012, 04:24 PM
  5. ORDER BY error when using DESC in an IIf
    By Smitoris in forum Queries
    Replies: 2
    Last Post: 10-30-2011, 02:48 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