Results 1 to 8 of 8
  1. #1
    MunroeM is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Nov 2015
    Posts
    55

    Query with 2 dates

    Hello all,

    I have a Query that has the following fields: Item Name, Retest Date and Expiration Date.

    I have a sub form on my main form that lists all the Item names and retest dates in order from current date to the future. I now need to add the Expiration date in there also but it only shows up at the top of the list if it is past the expiration date.

    example: (assume todays date is 7 Sept 16 for this example)
    Name - retest date - exp date
    Item-A 1-Jan-17 3-Sept-16
    Item-B 8-Sept-16 23-Feb-18
    Item-C 12-Sept-16 5-Dec-17
    Item-D 6-Oct-16 9-Jan-22

    So the list is sorting retest dates in order from now to future and if an expiration date is past todays date it will move it to the top of the list even if the retest date is further in the future then the ones on the top of the list.



    Any idea how I can do that. I have tried to add the expiration date on the query with a criteria of <=Date() but the list is blank as there is nothing expired right now but I still need the retest dates to show up like before

    Thanks for the help
    Last edited by MunroeM; 09-08-2016 at 07:42 AM.

  2. #2
    Bulzie is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    Nov 2015
    Posts
    1,511
    Confused on what you are asking. How do you want it to sort? Is the example above how you want it to work or how it is working now and incorrect? Are you sorting each field in your query? Are all 3 of those fields in the same table?

  3. #3
    MunroeM is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Nov 2015
    Posts
    55
    How it is currently working is its sorting the retest dates in ascending order and just displaying the expiration dates beside the retest dates.

    How I would like it to work is the retest dates are still in ascending order BUT if an expiration date is past the current date I want it to pop to the top of the list. In the example you can see Item B,C, and D are sorting according to the retest date BUT Item-A is at the top of the list because the Expiration Date is past the current date

    All fields are in the same table.

    Hope that clears up what I'm asking.

  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,652
    Add a field to the query:

    IIf(ExpDate<=Date(), 1,2)

    and have the primary sort on that field.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    MunroeM is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Nov 2015
    Posts
    55
    I put the IIf([ExpDate]<=Date(), 1,2) in the criteria section of the ExpDate field but nothing will show up when the query runs.

    What does the 1 and 2 do for the true and false statements in the IIF?

    This is my SQL statement for the query if that helps

    SELECT ItemTbl.[ItemName], ItemTbl.[RetestDate], ItemTbl.[ExpDate]
    FROM ItemTbl
    WHERE (((ItemTbl.[ExpDate])=IIf([ExpDate]<=Date(),1,2)))
    ORDER BY ItemTbl.[RetestDate], ItemTbl.[ExpDate];

  6. #6
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    I didn't say add it to the criteria, I said "add a field". More like:

    SELECT ItemTbl.[ItemName], ItemTbl.[RetestDate], ItemTbl.[ExpDate], IIf([ExpDate]<=Date(),1,2) As SortField
    FROM ItemTbl
    ORDER BY IIf([ExpDate]<=Date(),1,2), ItemTbl.[RetestDate], ItemTbl.[ExpDate];
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  7. #7
    MunroeM is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Nov 2015
    Posts
    55
    BAM!!! that worked like a charm.

    Thanks for the help

  8. #8
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    Happy to help!
    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: 5
    Last Post: 07-12-2014, 02:55 PM
  2. Replies: 5
    Last Post: 01-29-2014, 02:42 PM
  3. Replies: 8
    Last Post: 12-02-2013, 03:46 PM
  4. Replies: 1
    Last Post: 10-18-2013, 03:14 PM
  5. Query input dates for range of dates in two fields
    By kagoodwin13 in forum Queries
    Replies: 3
    Last Post: 02-13-2012, 04:25 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