Use the Right Cursor Type to get ADO Record Counts in ASP

If you are trying to count the number of records within a recordset but you’re getting back the value -1, then chances are you’re using the wrong cursor type.
In ActiveX Data Objects (ADO) you can only get a valid record count with static and dynamic cursors. Unfortunately, neither of these is the default cursor. Active Server Pages (ASP) doesn’t provide the ADO values. You have to include the correct values for a static or dynamic cursor.The following code shows how to retrieve an accurate record count from an Access database. Notice that the first two lines of code declare the values for the acceptable cursor types.

 <% Const adOpenDynamic = 2Const adOpenStatic = 3cn = "Provider=MSDASQL.1;"cn = cn & "Persist Security Info=False;"cn = cn & "User ID=admin;Connect Timeout=15;"cn = cn & "Extended Properties=""DBQ=d:mydb.mdb;"cn = cn & "DefaultDir=d:;"cn = cn & "Driver={Microsoft Access Driver (*.mdb)};"cn = cn & "DriverId=25;FIL=MS Access;MaxBufferSize=2048;"cn = cn & "MaxScanRows=8;PageTimeout=5;SafeTransactions=0;"cn = cn & "Threads=3;UID=admin;UserCommitSync=Yes;"";"cn = cn & "Locale Identifier=1033;User Id=admin;"sqlquery="SELECT category FROM mytable"Set rsdoc = Server.CreateObject("Adodb.Recordset")rsdoc.open sqlquery, cn, adOpenStaticnumrecs=rsdoc.recordcountResponse.Write numrecsrsdoc.closeSet rsdoc=NothingSet obJdbConnection=Nothing%>

Many programmers use an include file, adovbs.inc, which provides all of the constants for ADO programming.

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