Results 1 to 11 of 11
  1. #1
    kstyles is offline Novice
    Windows XP Access 2007
    Join Date
    Dec 2010
    Posts
    24

    Selecting Earliest and latest date

    I have a table called notes. It tracks the note date and the note corresponding to a specific annual review. IE - there is only one annual review number (in another table as the key) and many notes. I want to make a query to select the annual review number and the last note date and the earliest note date so I can make a report. I have tried using the SQL to input Max for the note date... so far I keep getting error messages. If I can make two seperate queries- one for the latest note date and one for the earliest that may be better and fit into my report scheme.

    Any help would be greatly appreciated!

  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,521
    Try

    SELECT ReviewNumber, Max(NoteDate) AS LastDate, Min(NoteDate) AS FirstDate
    FROM TableName
    GROUP BY ReviewNumber

    You'll obviously have to input the correct names. If you want other info, this might help:

    http://www.baldyweb.com/LastValue.htm
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    ssanfu is offline Master of Nothing
    Windows 2K Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    If you have a form with a button to open the report, add two text boxes.
    Name one dteFrom and the other dteTo.

    The query for the report would look like:

    Code:
    SELECT ReviewNumber, NoteDate, Notes
    FROM TableName
    WHERE [NoteDate] >= Forms!FormName.dteFrom AND [NoteDate]<= Forms!FormName.dteTo 
    ORDER BY [NoteDate]
    Change "FormName" to the name of your form.

  4. #4
    kstyles is offline Novice
    Windows XP Access 2007
    Join Date
    Dec 2010
    Posts
    24
    Okay... so this is the same problem I was running into when I tried this by myself before... I keep getting a syntax error...

    SELECT [Note Table].[Annual Review Number], Max[Note Table].[Note Date], Min[Note Table].[Note Date]
    FROM [Note Table];
    GROUP BY [Note Table].[Annual Review Number]

    I changed it up to reflect what I saw on the Baldy web site... and still nothing. What am I missing?

  5. #5
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Both Min and Max are functions that require parentheses around the field names.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  6. #6
    kstyles is offline Novice
    Windows XP Access 2007
    Join Date
    Dec 2010
    Posts
    24
    Okay... I am very lost here... I've tried using the parentheses and I am just not figuring this out! I tried changing the square brakets to parentheses and still get a syntax error message.

    SELECT (Max[Note Date]), (Min[Note Date])
    FROM [Note Table];
    GROUP BY Annual Review Number

    In reading the notes I have for SQL the operator is a verb and select is acceptable as the operator. Can you point on what I'm doing incorrect here?

    THANKS!!!!

  7. #7
    weekend00 is offline I may not be right
    Windows XP Access 2003
    Join Date
    Aug 2010
    Posts
    1,295
    a little bit changes:

    change:
    SELECT (Max[Note Date]), (Min[Note Date]) FROM [Note Table]; GROUP BY Annual Review Number
    to:
    SELECT Max([Note Date]), Min([Note Date]) FROM [Note Table] GROUP BY [Annual Review Number]

    Maybe you want to add [Annual Review Number] to the select list like this
    SELECT [Annual Review Number], Max([Note Date]), Min([Note Date]) FROM [Note Table] GROUP BY [Annual Review Number]

  8. #8
    kstyles is offline Novice
    Windows XP Access 2007
    Join Date
    Dec 2010
    Posts
    24
    Yeah!!!! Thank you - I can see now where the parentheses go! I got it to work!

    SELECT Max([Note Table].[Note Date]) AS [MaxOfNote Date], Min([Note Table].[Note Date]) AS [MinOfNote Date], [Note Table].[Annual Review Number]
    FROM [Note Table]
    GROUP BY [Note Table].[Annual Review Number];


    THANKS!!!

  9. #9
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    I assumed you would refer back to the sample SQL I posted, which showed the proper placement. Glad you got it sorted out.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  10. #10
    kstyles is offline Novice
    Windows XP Access 2007
    Join Date
    Dec 2010
    Posts
    24
    I tried it that way, but I didn't understand how the square brakets fit into the expression. I got myself confused... but now I'm happy to have it actually work!! I really appreciate the help I receive on here - makes things seem pretty easy once I have some input from everyone!

  11. #11
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    The square brackets are not normally required. They are required in your case because of the inadvisable spaces in your names. If it's not too late, I'd get rid of them; in the long run, they're not worth the trouble.
    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. How do you get the latest date?
    By radicrains in forum Queries
    Replies: 6
    Last Post: 10-27-2010, 08:59 PM
  2. Replies: 18
    Last Post: 08-22-2010, 05:22 AM
  3. Query for latest date? what am I doing wrong?
    By Overdive in forum Queries
    Replies: 1
    Last Post: 12-18-2009, 06:04 AM
  4. Query to find latest date
    By Lockrin in forum Access
    Replies: 2
    Last Post: 12-16-2009, 10:00 AM
  5. Latest Date Records
    By Rick West in forum Queries
    Replies: 1
    Last Post: 09-25-2009, 11:16 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