Important alert: (current site time 9/2/2014 9:50:11 PM EDT)
 

VB icon

Total Record Count In ADO Recordset

Email
Submitted on: 5/10/1999
By: Riaan Aspeling  
Level: Not Given
User Rating: By 39 Users
Compatibility: VB 5.0, VB 6.0, VB Script, ASP (Active Server Pages)
Views: 9491
 
     This simple little function just returns the total number of records in a ADO recordset.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
'**************************************
' Name: Total Record Count In ADO Recordset
' Description:This simple little function just returns the total number of records in a ADO recordset.
' By: 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.
'
'This code is copyrighted and has' limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=1742&lngWId=1'for details.'**************************************

'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


Other 23 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Not Given category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

8/12/1999 9:50:00 AMCorey

How about the RecordSet's RecordCount property?
(If this comment was disrespectful, please report it.)

 
8/12/1999 10:09:00 PMRiaan Aspeling

Hi Corey

That's quite a valid statement. It doesn't work with a SQL 6.5 or 7 recordset connection though. You have to move to the last record before the RecordCount property get's set!!??!!

That's why I wrote this silly little function.

Maybe it's a bug in the ADO layer?

Kind Regards
Riaan Aspeling
(If this comment was disrespectful, please report it.)

 
9/15/1999 11:36:00 AMJeremy

I wrote a couple functions that do the following....
First i dim a ADOConnection object as an array, and they do the same with a recordset. cn() and rs(). The functions merely return the index of the newly created connection or recordset. The connection part works fine, but when i load a record set into an array it seems to return an infinate number of records.
This make any sense to you?
(If this comment was disrespectful, please report it.)

 
1/27/2000 9:48:55 AMSteven Lee

RecordCount is set to 1 if SQL returns a least one row. You have to use MoveNext method to get to the end of the recordset before using RecordCount property. This is not a bug and it's true for all other database engines. Whenever possible, use the "Count(*)" SQL function instead of "MoveLast".
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.