Total Record Count In ADO Recordset

By | 2002-06-01

This simple little function just returns the total number of records in a ADO recordset.

Original Author: Riaan Aspeling

Inputs

A ADODB.Recordset structure

Returns

A Long integer with the total number of records

Side Effects

I believe it’s not the fastest way of retrieving the information but at least it works. I’d like it if somebody can suggest a alt. way of getting to this info.

Code

'Pass this function your ADO recordset
Function GetTotalRecords(ByRef aRS As ADODB.Recordset) As Long
On Error GoTo handelgettotalrec
Dim adoBookM As Variant 'Declare a variable to keep the current location
adoBookM = aRS.Bookmark 'Get the current location in the recordset
aRS.MoveLast   'Move to the last record in the recordset
GetTotalRecords = aRS.RecordCount 'Set the count value
aRS.Bookmark = adoBookM 'Return to the origanal record
Exit Function
handelgettotalrec:
GetTotalRecords = 0  'If there's any errors return 0
Exit Function
End Function

Author: dwirch

Derek Wirch is a seasoned IT professional with an impressive career dating back to 1986. He brings a wealth of knowledge and hands-on experience that is invaluable to those embarking on their journey in the tech industry.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.