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
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
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.