Results 1 to 5 of 5
  1. #1
    ammalo is offline Novice
    Windows 10 Access 2016
    Join Date
    May 2016
    Location
    Peoria, IL
    Posts
    3

    Select the first 3 (or any number) # of records from a table by date

    I create lists of songs for a band. I have a table showing what songs were played at each gig. I would like to create a query to identify what songs were included in the last 3 (or any number) of set lists. How do I create a query to do this?
    The table looks like this:



    When Location 1 2
    6/10/2016 12:00:00 AM Dummy I Can See Clearly Now It's Not Unusual
    5/17/2016 2:00:00 PM Bickford Cottage Candy Man, The Crazy
    4/27/2016 3:30:00 PM Independence Village 9 to 5 Beyond the Sea
    4/21/2016 1:30:00 PM Bickford Cottage Bushel and a Peck Copacabana
    4/18/2016 2:30:00 PM Hopedale Dancing In The Moonlight Desperado
    3/29/2016 1:30:00 PM Hillside Village 9 to 5 Amazing Grace
    3/17/2016 1:30:00 PM Bickford Cottage Crazy Hit the Road Jack
    1/27/2016 3:30:00 PM Independence Village 9 to 5 Bring It On Home To Me
    1/21/2016 2:00:00 PM Bickford Cottage Beyond the Sea Daddy's Little Girl

  2. #2
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,430
    I can't make sense of your data - don't understand what columns 1 and 2 are (looks like unnormalised data), but you would use TOP and ORDER BY e.g.

    SELECT TOP 3 *
    FROM tblSongs
    ORDER BY WHEN Desc

  3. #3
    ammalo is offline Novice
    Windows 10 Access 2016
    Join Date
    May 2016
    Location
    Peoria, IL
    Posts
    3
    The first column is the date and time. To present it here, I cut and pasted through Excel to make it readable. The "TOP" function is the part I was missing.

  4. #4
    ammalo is offline Novice
    Windows 10 Access 2016
    Join Date
    May 2016
    Location
    Peoria, IL
    Posts
    3
    Thanks, Ajax. This gives me what I was looking for. Before this tip, I was setting a design criteria prompt to ask how many days past to include. Is there a way to do something similar for the TOP value, so that the program would ask how many TOP records to include?

  5. #5
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,430
    Is there a way to do something similar for the TOP value, so that the program would ask how many TOP records to include?
    in terms of an existing query to run, no. If on the other hand you have vba code to create and run your query, then yes. something like


    sqlstr="SELECT TOP " & me.txtTopNo & " * FROM......"


    where me.txtTopNo is the name of the control the user has entered the number of records required.

    then assign the string to a query, a form or report recordsource, a combo or listbox rowsource

    Not that TOP 3 can produce more or less records - if there are only 2 in the recordset then only 2 records are returned. if the values are say

    1
    1
    2
    2
    2
    3

    then 4 records will be returned - 1,1,2,2,2

    to get round this, you may need an additional column to order by

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

Similar Threads

  1. Replies: 3
    Last Post: 02-26-2016, 01:03 PM
  2. Table select field shows up as a number ot text
    By LaughingBull in forum Access
    Replies: 2
    Last Post: 09-04-2015, 12:33 AM
  3. select the 2 earliest date records for each id
    By lbrannon in forum Queries
    Replies: 10
    Last Post: 07-14-2015, 04:10 PM
  4. Replies: 2
    Last Post: 05-28-2013, 04:00 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