Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Web
Expertise: Intermediate
Apr 27, 2000

Counting the Number of Records in a Recordset

One of the handy features of working with the ActiveX Data Object (ADO) is the ability to load the results of SQL select statements into a Recordset. Once you have loaded the Recordset, you can display, search, and manipulate the data pulled from a data store. Unfortunately, finding out how many records are in a Recordset is not as easy as it should be.

The Recordset object, created by the Open or Execute functions of the ADODB object, has a RecordCount property, but this property reports '-1' if the Recordset has been opened with a forward-only cursor. Since this is the default and the only way that you can open a Recordset if you have created it using an Execute command, you will need to have another way to count the records.

One simple way is to create a counter that you increment as you step through your Recordset, as shown in the following code segment:

 
<%
RecordCount = 0
Set objRecordset=MyConn.Execute(Query)
WHILE NOT objRecordset.EOF
RecordCount = RecordCount + 1
Response.Write(objRecordset("title")&"<BR>")
objRecordset.MoveNext
WEND
%>
<%= RecordCount %> records in the database.

UPDATE 6/16/00: Francisco Mejia, from Moorestown, New Jersey, contributed this alternative way to count recordsets:

You can also count recordsets using 'recordset.GetRows'

  
dim objRecordset as ado.recordset
dim RecordCount = 0
 
set  objRecordset=MyConn.Execute(Query)

RecordCount =  ubound(rs.getrows)- 1
 

Manoj K.
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap