Solved

Retrieve most current records

Posted on 2011-03-22
3
231 Views
Last Modified: 2012-05-11
I currently have a very simple Access query that retrieves all records within a date range.
I frequently have to run it multiple times to wind up with the approximate number of records I'm looking for in a given situation.

As an example, let's say on a given day, I'm interested in retrieving about 500 records.

What I currently do is just guesstimate from experience that within a 1 month period, there will be about 500 records returned.  So, I set up the query to retrieve, say November 1, 2010 through November 30, 2010 data and see how many records get returned.  What frequently happens is too few records may be returned or too many records (depending on what date range I use). Using the November 1 thru November 30 example above, let's say the query returns only 200 records. What I wind up then doing is rerunning the query with an expanded date range (say October 1, 2010 thru November 30, 2010) to see if that gets me closer to 500 records. Etc Etc.

What would be ideal (and that I of course don't know how to do; that's why I'm asking) is to be able to just query a wide date range where I'm almost always guaranteed to be returned at least as many records as I'm looking for.  Within that same query, then retrieve the MOST CURRENT, say 500 records from whatever records were returned.

In other words, let's say I query a wide date range such as 1/1/20 thru 12/31/10.  Let's say that query returns something huge such as 100,000 records.  I don't want all 100,000 records. I just want the most current 500. By most current I mean most current datewise (My table has a date field).   Any easy way to do this?
0
Comment
Question by:dbfromnewjersey
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 4

Accepted Solution

by:
philetaylor earned 125 total points
ID: 35193412
Something like:

SELECT TOP 500 * from tablename ORDER BY datefield DESC

this will return the last 500 records. If you want to only return records before a particular date, you could do:

SELECT TOP 500 * from tablename WHERE datefield < #2010-01-01# ORDER BY datefield DESC

You can replace the * with a list of fields (fielda,fieldb,fieldc etc)

Cheers

Phil
0
 
LVL 12

Assisted Solution

by:telyni19
telyni19 earned 125 total points
ID: 35193624
You can specify in your query a certain fixed number of records to return, as Phil described, but you can't directly set the number as a variable or parameter. In order to choose how many records to return without modifying the query directly, you would need to have a form with an input field and have the query programmatically generated through SQL. See attached for an example of how to do that.

The dates to filter the results by could also be added programmatically from input fields on the form, making the query even more versatile.
Private Sub Command2_Click()
Dim strsql As String
Dim dbCur As Database
Dim qryTop As QueryDef
Dim intRecs As Integer

Set dbCur = CurrentDb

intRecs = Int(Nz(Me.txtRecCount.Value, 0))

If intRecs > 0 Then
strsql = "SELECT TOP " & intRecs & _
" TableData.ID, TableData.NumberVal, TableData.CreationDate" & _
" FROM TableData WHERE (((TableData.CreationDate) Between #1/1/2011#" & _
" And #2/28/2011#)) ORDER BY TableData.CreationDate DESC;"

Set qryTop = dbCur.QueryDefs("TopRecordsbyDate")

qryTop.SQL = strsql
DoCmd.OpenQuery "TopRecordsbyDate"
End If

Set qryTop = Nothing
Set dbCur = Nothing
End Sub

Open in new window

TopCountExample.zip
0
 

Author Comment

by:dbfromnewjersey
ID: 35207529
Thank you very much
0

Featured Post

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question