Results 1 to 5 of 5
  1. #1
    messier is offline Novice
    Windows 10 Access 2016
    Join Date
    Mar 2020
    Posts
    18

    Query Records of Max Date

    I have a linked table of which another department updates on a monthly basis. Each month they will update the total number of customers.

    Eg,.
    31 Dec 2019 - 8 records
    31 Jan 2020 - 10 records
    29 Feb 2020 - 9 records
    31 Mar 2020 - 12 records
    (total rows in table 39)



    I would like to pull only the 12 latest records. However, dynamically. I don't want to know what is the latest date, the query should automatically know that 31 Mar 2020 is the lasrest and pull the 12 records only.

    I'm using the simple) query design and not writing codes.


    Thanks in advance.
    Last edited by messier; 04-25-2020 at 05:14 PM.

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    Put a descending sort on the date field, and in the query properties set the top values property to 12. In SQL view it will look like:

    SELECT TOP 12 DateField, NumberField
    FROM TableName
    ORDER BY DateField DESC
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    messier is offline Novice
    Windows 10 Access 2016
    Join Date
    Mar 2020
    Posts
    18
    I would like not to count how many records. Ideally, my query should dynamically pull the number of records automatically based on latest date.

  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
    Ah, well, I might do it dynamically by perhaps:

    SELECT DateField, NumberField
    FROM TableName
    WHERE DateField = (SELECT Max(DateField) FROM TableName)
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    messier is offline Novice
    Windows 10 Access 2016
    Join Date
    Mar 2020
    Posts
    18
    Thank you. Let me try this later. I think I saw it worked (had to leave my workstation).

    However, can you share, why my criteria for DateField cannot be Max(DateField)?
    I would like to understand the difference. Thank you!

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

Similar Threads

  1. Replies: 1
    Last Post: 10-12-2017, 07:19 PM
  2. Replies: 1
    Last Post: 04-16-2014, 04:25 PM
  3. Replies: 3
    Last Post: 03-11-2014, 07:32 PM
  4. Replies: 5
    Last Post: 12-12-2013, 12:14 PM
  5. Replies: 1
    Last Post: 05-09-2013, 09:13 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