devxlogo

Counting the Number of Records in a Recordset

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 = 0Set objRecordset=MyConn.Execute(Query)WHILE NOT objRecordset.EOFRecordCount = RecordCount + 1Response.Write(objRecordset("title")&"
")objRecordset.MoveNextWEND%>

<%= 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.recordsetdim RecordCount = 0 set  objRecordset=MyConn.Execute(Query)RecordCount =  ubound(rs.getrows)- 1 

See also  Comparing different methods of testing your Infrastructure-as-Code
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist