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 

Share the Post:
data observability

Data Observability Explained

Data is the lifeblood of any successful business, as it is the driving force behind critical decision-making, insight generation, and strategic development. However, due to its intricate nature, ensuring the

Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved