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:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS

homes in the real estate industry

Exploring the Latest Tech Trends Impacting the Real Estate Industry

The real estate industry is changing thanks to the newest technological advancements. These new developments — from blockchain and AI to virtual reality and 3D printing — are poised to change how we buy and sell homes. Real estate brokers, buyers, sellers, wholesale real estate professionals, fix and flippers, and beyond may