ID: Q105976
The information in this article applies to:
Moderate: Requires basic macro, coding, and interoperability skills.
The RecordCount property, when used with a recordset or snapshot, returns a recordset that has an incorrect number of records.
This article assumes that you are familiar with Visual Basic for Applications and with creating Microsoft Access applications using the programming tools provided with Microsoft Access. For more information about Visual Basic for Applications, please refer to your version of the "Building Applications with Microsoft Access" manual.
NOTE: Visual Basic for Applications is called Access Basic in Microsoft Access versions 1.x and 2.0. For more information about Access Basic, please refer to the "Introduction to Programming" manual in Microsoft Access version 1.x or the "Building Applications" manual in Microsoft Access version 2.0.
For recordsets and snapshots, Microsoft Access does not automatically return the number of records that exist in the recordset. Rather, it returns the number of records accessed.
To determine the exact number of records in a recordset or snapshot, use the MoveLast method before checking the RecordCount property.
This behavior is by design.
The following Visual Basic function, MyWrongRecordCount(), returns the number 1 for the Customers table in the sample database Northwind.mdb (or NWIND.MDB in versions 1.x and 2.0) because only one record has been accessed. The MyRightRecordCount() function uses the MoveLast method first to access all records in the recordset and then to return the RecordCount value.
1. Open the sample database Northwind.mdb (or NWIND.MDB in versions 1.x
and 2.0).
2. Create a module and type the following line in the Declarations
section if it is not already there:
Option Explicit
3. Type the following procedures:
In Microsoft Access 7.0 and 97:
'===========================================================
' 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
4. To test these functions, type the following lines in the Debug window
(or Immediate window in versions 1.x and 2.0), and then press ENTER
after you've entered each one:
?MyWrongRecordCount()
Note that the function returns 1.
?MyRightRecordCount()
Note that the function returns the correct number of records in the
Customers table.
For more information about RecordCount Property, search the Help Index for "RecordCount Property," or ask the Microsoft Access 97 Office Assistant.
Additional query words: record count move last
Keywords : kbprg PgmObj
Version : 1.0 1.1 2.0 7.0 97
Platform : WINDOWS
Hardware : x86
Issue type : kbprb
Last Reviewed: November 21, 1998