ACC: RecordCount Property Returns Incorrect Number of Records

Last reviewed: August 29, 1997
Article ID: Q105976
The information in this article applies to:
  • Microsoft Access versions 1.0, 1.1, 2.0, 7.0, 97

SYMPTOMS

Moderate: Requires basic macro, coding, and interoperability skills.

The RecordCount property, when used with a recordset or snapshot, returns a recordset containing 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.

CAUSE

For recordsets and snapshots, Microsoft Access does not automatically return the number of records that exist in the recordset. It returns the number of records accessed.

RESOLUTION

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

STATUS

This behavior is by design.

MORE INFORMATION

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 dynaset and then to return the RecordCount value.

Steps to Reproduce Behavior

  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:

          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.

          ?MyWrongRecordCount()
    

    Note that the function returns 1.

          ?MyRightRecordCount()
    

    Note that the function returns the correct number of records in the Customers table.

REFERENCES

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 : JetRS kbprg PgmObj
Version : 1.0 1.1 2.0 7.0 97
Platform : WINDOWS
Hardware : x86
Issue type : kbprb
Solution Type : Info_Provided


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: August 29, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.