The information in this article applies to:
SYMPTOMSThe RecordCount property, when used with a Dynaset or Snapshot, can sometimes return an incorrect number of records. This applies to the Microsoft Access database engine that is built into Visual Basic version 3.0. CAUSEFor Dynasets and Snapshots, the RecordCount property does not automatically return the number of records that exist in the recordset. RecordCount returns the number of records accessed. If you don't do a MoveLast method immediately before checking the record count, you will get an incorrect, smaller count. RESOLUTION
To determine the number of records in a Dynaset or Snapshot, use the
MoveLast method before checking the RecordCount property. This
requirement is by design.
STATUSThis behavior is by design because otherwise, Visual Basic would have to do an implicit MoveLast. This would be very slow with large record sets and especially with remote databases, so the decision is left up to the programmer. MORE INFORMATION
NOTE: If your data is displayed in a Grid control, the RecordCount will
be one greater than the last line number in the grid because the grid
starts at zero.
How to Count Records in Whole Table Quickly by Using ListTables MethodThe following steps count the number of records in a table without opening the table:
How to Count Records in Snapshot, Dynaset, or Data ControlNOTE: A MoveLast will be slow on a large table or set. Only use the method in the section below for counting subsets of the table. To count the number of records that comprise the whole table, use the ListTables Method given above.If you are using a Snapshot, Dynaset, or the data control, you can count the records in the current recordset by first doing a MoveLast. Then use the RecordCount property. This count is only accurate for that instant, because another user could be simultaneously adding or deleting records to the underlying table. By design, a data control is linked to a Dynaset. A MoveLast on a recordset variable (a Dynaset or Snapshot) is faster than MoveLast on a data control. You can create a separate Snapshot variable of your data control's recordset and invoke a MoveLast on that Snapshot. The following program shows how to use MoveLast and the RecordCount property to count the number of records in a Dynaset.
Records Must Be Properly Added Before They Are CountedThe Addnew method allocates space for a new record in your database. You then add data to the various table fields in the new record. You then do an Update method to write the new record to the table.The Update method saves the contents of the copy buffer to a specified Table or Dynaset. Use Update to save any changes to a record after using Edit or AddNew. With a data control, if an Edit or AddNew operation is pending when you move to another record or close the recordset, Update is automatically invoked if not stopped during the Validate event. NOTE: In the Professional Edition, if you are not using a data control and move to another record or close the recordset while an Edit or AddNew operation is pending, any existing changes will be lost and no error will occur. Loops and RecordCountUse EOF instead of RecordCount in loops. For example, don't use this:
Use the following instead:
REFERENCES
Additional query words: 3.00
Keywords : kbcode |
Last Reviewed: June 21, 1999 © 2000 Microsoft Corporation. All rights reserved. Terms of Use. |