Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

move last , move first

Status
Not open for further replies.

merlin72

Programmer
Apr 18, 2000
50
US
Set rst = db.OpenRecordset(SQL)
rst.MoveLast
rst.MoveFirst

Is it manditory , for access to work correctly , to have movelast first. I take it that these commands tells access to look at all records.

 
It's been my understanding that you movelast the movefirst to get an accurate record count.
 
It is my understanding that Access already knows the record count in a rst, in
recsetname.RecordCount.

alley
 
I tried both in Access 97
method 1
Set rst = db.OpenRecordset(SQL)
intA = rst.recordcount
msgbox intA

method 2
Set rst = db.OpenRecordset(SQL)
rst.MoveLast
rst.MoveFirst
intA = rst.recordcount
msgbox intA


and got 2 different numbers. Let me know what your results are

 
Both methods returned the same number. My rst is small (0-8), but don't see that
as making a difference

alley
 
Merlin,

If by "work correctly you mean that you can get an accurate record count, then yes as Disco stated. In Access 97, the RecordCount property will be either 0 or 1 after first creating the recordset. 0 is empty and 1 is 1 record or more. If you MoveLast / MoveFirst access then reads through all records and sets RecordCount = the actual number of records in the recordset and returns to the first record.

My customers table has 5 customers in the state of PA ...

Ex 1

Dim R as Recordset, SQLTxt
SQLText = "Select * From Customers Where State = 'PA';"
Set R = CurrentDB.OpenRecordset(SQLTxt)
MsgBox "Records = " & R.RecordCount

Result --> Records = 1 (even though there are 5 records)

EX 2

Dim R as Recordset, SQLTxt
SQLText = "Select * From Customers Where State = 'PA';"
Set R = CurrentDB.OpenRecordset(SQLTxt)
R.MoveLast
R.MoveFirst
MsgBox "Records = " & R.RecordCount

Result --> Records = 5

Also MoveLast will generate an error message if the recordset is empty so its a good idea to test for an empty record set before attempting to moveLast.
...
If R.RecordCount Then
Set R = CurrentDB.OpenRecordset(SQLTxt)
R.MoveLast
R.MoveFirst
Else
MsgBox " There are no matching records"
End IF

Alley, what version of Access ? Im curious.

Dave
 
Thank y'all very much . This has explained alot. I take it that you must do a moveLast then movefirst to get an accurate record count.
 
I stopped doing the MoveLast in Access 2000. In Access 97 I did the MoveLast/MoveFirst but with Access 2000 it works to directly reference the record count. These are either DAO or ADODB recordsets. Also, I directly reference the rst.BOF and rst.EOF properties and it works as expected.
 
I always do the following:
function counter()
dim dbs as database, qdf as querydef, rst as recordset, number_of_records&
set dbs = currentdb
set qdf = dbs.createquerydef("SOME_QUERY_NAME","SELECT * FROM [TableName];")
set rst = dbs.openrecordset("SOME_QUERY_NAME")
rst.movefirst
number_of_records = rst.fields(0)
docmd.deleteobject acquery, "SOME_QUERY_NAME"
end function

Cheers
Chris
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top