Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: Web
Expertise: Beginner
Sep 20, 1999

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 = 2
Const adOpenStatic = 3
cn = "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, adOpenStatic
Response.Write numrecs
Set rsdoc=Nothing
Set obJdbConnection=Nothing
Many programmers use an include file, adovbs.inc, which provides all of the constants for ADO programming.
Ken Cox
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date