William Benson (VBACreations.Com)
vbacreations at gmail.com
Fri Jun 3 02:52:36 CDT 2011
Hi Darrell,
I am still trying to digest most of your reply, thanks, but in the meantime
Let me make sure we understand one another.
My objective is to find the latest occurring record where (for example) Name
= "Bill" and Occupation = "Accountant"; Suppose I have 3 records like this,
timestamped
Name Occupation WhenRecordImported
Bill Accountant 1/15/2011 3:52AM
Bill Accountant 1/17/2011 1:52AM
Bill Accountant 1/22/2011 2:52AM
When you say index on WhenRecordImported and use SEEK, what is driving your
example date of 5/10/2011 - why are you picking that? I have no knowledge of
what timestamps are in the table when I employ this device.
Thanks.
-----Original Message-----
From: accessd-bounces at databaseadvisors.com
[mailto:accessd-bounces at databaseadvisors.com] On Behalf Of Darrell Burns
Sent: Thursday, June 02, 2011 9:21 PM
To: 'Access Developers discussion and problem solving'
Subject: Re: [AccessD] Most efficient means to retrieve most recent record
Hi William.
I think you'd get the fastest retrieval by putting an index on your
timestamp field. Let's call it "ixTimeStamp". Then after you set your
recordset, set the index as...
1. rst.index = "ixTimeStamp"
This will sort your recordset on the index. Then do a seek. For example, to
search the recordset for stuff that happened on 5/10/2011...
2. rst.seek ">=", #5/10/2011# (I think that's the correct syntax for a date
value)
Then start a loop on the recordset and exit the loop when the date
changes...
3. do while not rst.eof
If rst!TimeStamp > #5/10/2011# then
Exit do
You may also want to consider splitting the date and time fields. I usually
convert my dates to a YYYYMMDD string format and index the string field. For
example...
Rst.index = "ixStrDate"
Rst.seek, "=", "20110510"
After you do a seek, you have to test for a NoMatch. For example...
Rst.index = "ixStrDate"
Rst.seek "=", "20110602"
If NOT rst.nomatch then
Do While not rst.eof
If rst!strDate > "20110602" then
Exit do
End if
<your code>
Rst.movenext
Loop
End If
Sorry bout dat.
Darrell
-----Original Message-----
From: accessd-bounces at databaseadvisors.com
[mailto:accessd-bounces at databaseadvisors.com] On Behalf Of William Benson
(VBACreations.Com)
Sent: Thursday, June 02, 2011 5:12 PM
To: 'Access Developers discussion and problem solving'
Subject: [AccessD] Most efficient means to retrieve most recent record
Hello,
In an Access table where I import and append very similar data routinely, I
stamp every record with the same ImportTime at time of import.
I am wondering the most efficient means of getting at the latest record
matching certain criteria? I see three ways.
METHOD 1
(1) Set a recordset = the entire table ordered descending, with no WHERE
clause
(2) use Rst.FindFirst and put the criteria here as arguments
METHOD 2
(1) Use an ORDER BY (desc) clause, select all the records
where those fields have the values I specify
(2) Use the first record in the recordset.
METHOD 3
(1) Select the max date where those fields have the values I specify
(2) Select "the" record where those fields have the values I specify
AND
Date = Date found from (1) .... to me this seems unlikely to be
fastest, but who knows.
--
AccessD mailing list
AccessD at databaseadvisors.com
http://databaseadvisors.com/mailman/listinfo/accessd
Website: http://www.databaseadvisors.com
--
AccessD mailing list
AccessD at databaseadvisors.com
http://databaseadvisors.com/mailman/listinfo/accessd
Website: http://www.databaseadvisors.com