Microsoft KB Archive/105976

From BetaArchive Wiki
< Microsoft KB Archive
Revision as of 09:57, 20 July 2020 by X010 (talk | contribs) (Text replacement - """ to """)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Article ID: 105976

Article Last Modified on 1/18/2007



APPLIES TO

  • Microsoft Access 1.0 Standard Edition
  • Microsoft Access 1.1 Standard Edition
  • Microsoft Access 2.0 Standard Edition
  • Microsoft Access 95 Standard Edition
  • Microsoft Access 97 Standard Edition



This article was previously published under Q105976

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

SYMPTOMS

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.

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.

RESOLUTION

To determine the exact number of records in a recordset 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 recordset 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 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.


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: kbprb kbprogramming KB105976