Article ID: 105976
Article Last Modified on 1/18/2007
Option Explicit
'===========================================================
' The following function, MyWrongRecordCount(), demonstrates
' the incorrect way to use the RecordCount property to count
' records in a dynaset.
'===========================================================
Function MyWrongRecordCount ()
Dim MyDB As Database
Dim MyRS as Recordset
Set MyDB = CurrentDB()
Set MyRS = MyDb.OpenRecordset("Customers", dbOpenDynaset)
MyWrongRecordCount = MyRS.RecordCount
MyRS.Close
End Function
'===========================================================
' The following function, MyRightRecordCount(), demonstrates
' the correct way to use the RecordCount property to count
' records in a dynaset.
'===========================================================
Function MyRightRecordCount ()
Dim MyDB As Database
Dim MyRS as Recordset
Set MyDB = CurrentDB()
Set MyRS = MyDb.OpenRecordset("Customers", dbOpenDynaset)
MyRS.MoveLast
MyRightRecordCount = MyRS.RecordCount
MyRS.Close
End Function
In Microsoft Access 2.0:
'===========================================================
' The following function, MyWrongRecordCount(), demonstrates
' the incorrect way to use the RecordCount property to count
' records in a dynaset.
'===========================================================
Function MyWrongRecordCount ()
Dim MyDB As Database
Dim MyRS as Recordset
Set MyDB = CurrentDB()
Set MyRS = MyDb.OpenRecordset("Customers", DB_OPEN_DYNASET)
MyWrongRecordCount = MyRS.RecordCount
MyRS.Close
End Function
'===========================================================
' The following function, MyRightRecordCount(), demonstrates
' the correct way to use the RecordCount property to count
' records in a dynaset.
'===========================================================
Function MyRightRecordCount ()
Dim MyDB As Database
Dim MyRS as Recordset
Set MyDB = CurrentDB()
Set MyRS = MyDb.OpenRecordset("Customers", DB_OPEN_DYNASET)
MyRS.MoveLast
MyRightRecordCount = MyRS.RecordCount
MyRS.Close
End Function
In Microsoft Access 1.x:
'===========================================================
' The following function, MyWrongRecordCount(), demonstrates
' the incorrect way to use the RecordCount property to count
' records in a dynaset.
'===========================================================
Function MyWrongRecordCount ()
Dim MyDB As Database
Dim MyRS as Dynaset
Set MyDB = CurrentDB()
Set MyRS = MyDb.CreateDynaset("Customers")
MyWrongRecordCount = MyRS.RecordCount
MyRS.Close
End Function
'===========================================================
' The following function, MyRightRecordCount(), demonstrates
' the correct way to use the RecordCount property to count
' records in a dynaset.
'===========================================================
Function MyRightRecordCount ()
Dim MyDB As Database
Dim MyRS as Dynaset
Set MyDB = CurrentDB()
Set MyRS = MyDb.CreateDynaset("Customers")
MyRS.MoveLast
MyRightRecordCount = MyRS.RecordCount
MyRS.Close
End Function
?MyWrongRecordCount()
Note that the function returns 1.
?MyRightRecordCount()
Note that the function returns the correct number of records in the
Customers table.Additional query words: record count move last
Keywords: kbprb kbprogramming KB105976