PRB: Using RecordCount with VB Dynasets, Snapshots, and TablesID: Q109053
|
The 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.
For 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.
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.
NOTE: If you add or delete records to a table within a transaction, and
then roll back the transaction, the value of the RecordCount property is
not adjusted accordingly.
This 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.
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.
Sub Form_Load ()
Const DB_TABLE = 1 ' Constant taken from DATACONS.TXT file.
Dim db As Database
Dim snap As Snapshot
Set db = OpenDatabase("C:\VB3\BIBLIO.MDB")
Set snap = db.ListTables() ' Copy Table information to Snapshot.
Do While Not snap.EOF
If snap("TableType") = DB_TABLE Then
'Enter the Table name for which you want a record count:
If snap("Name") = "Authors" Then
MyRecordCount = snap("RecordCount")
End If
End If
snap.MoveNext ' Move to next record.
Loop
snap.Close
form1.Show ' Must Show form1 in Load event before Print works.
Print MyRecordCount
End Sub
Sub Form_Load ()
Dim MyDB As Database, MyDyna As Dynaset
Set MyDB = OpenDatabase("C:\VB3\BIBLIO.MDB")
Set MyDyna = MyDB.CreateDynaset("Authors")
MyDyna.MoveLast
MyRecordCount = MyDyna.RecordCount
MyDyna.Close
form1.Show
Print MyRecordCount
End Sub
For i = 1 to ds.RecordCount ' Bad code
...
Next
Do Until ds.EOF ' Good code
...
Loop
Additional query words: 3.00
Keywords : kbcode
Version : 3.00
Platform : WINDOWS
Issue type :
Last Reviewed: June 21, 1999