devxlogo

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

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.

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