Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

ACC2000: RecordCount Property Returns Incorrect Number of Records


View products that this article applies to.

This article was previously published under Q207652
Moderate: Requires basic macro, coding, and interoperability skills.

This article applies to a Microsoft Access database (.mdb) and to a Microsoft Access project (.adp).

↑ Back to the top


Symptoms

The RecordCount property, when used with a recordset or snapshot, returns a recordset that has an incorrect number of records.

↑ Back to the top


Cause

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.

↑ Back to the top


Resolution

To determine the exact number of records in a recordset or snapshot, use the MoveLast method before checking the RecordCount property.

↑ Back to the top


More information

The following Visual Basic function, MyWrongRecordCount(), returns the number 1 for the Customers table in the sample database Northwind.mdb 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.

NOTE: The sample code in this article uses Microsoft Data Access Objects. For this code to run properly, you must reference the Microsoft DAO 3.6 Object Library. To do so, click References on the Tools menu in the Visual Basic Editor, and make sure that the Microsoft DAO 3.6 Object Library check box is selected.

Steps to Reproduce Behavior

1.Open the sample database Northwind.mdb.
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:
'===========================================================
' The following function, MyWrongRecordCount(), demonstrates
' the incorrect way to use the RecordCount property to count
' records in a dynaset.
'===========================================================

Function MyWrongRecordCount ()
   Dim MyDB As DAO.Database
   Dim MyRS as DAO.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 DAO.Database
   Dim MyRS as DAO.Recordset
   Set MyDB = CurrentDB()
   Set MyRS = MyDb.OpenRecordset("Customers", dbOpenDynaset)
   MyRS.MoveLast
   MyRightRecordCount = MyRS.RecordCount
   MyRS.Close
End Function
					
4.To test these functions, type the following lines in the Immediate Window, 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.

↑ Back to the top


References

For more information about the RecordCount property, in the Visual Basic Editor, click Microsoft Visual Basic Help on the Help menu, type RecordCount property in the Office Assistant or the Answer Wizard, and then click Search to view the topic.

↑ Back to the top


Keywords: KB207652, kbvba, kbprogramming, kbprb, kbofficeprog, kbdta

↑ Back to the top

Article Info
Article ID : 207652
Revision : 3
Created on : 10/11/2006
Published on : 10/11/2006
Exists online : False
Views : 326