?
Solved

Retrieve most current records

Posted on 2011-03-22
3
Medium Priority
?
232 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 500 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 500 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

In earlier versions of Windows (XP and before), you could drag a database to the taskbar, where it would appear as a taskbar icon to open that database.  This article shows how to recreate this functionality in Windows 7 through 10.
The Windows Phone Theme Colours is a tight, powerful, and well balanced palette. This tiny Access application makes it a snap to select and pick a value. And it doubles as an intro to implementing WithEvents, one of Access' hidden gems.
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
Suggested Courses

770 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