views:

486

answers:

3

When ever I try to access the RecordCount property, I always get a return value of -1. Below is my sample code.

Set oConn = Server.CreateObject ("ADODB.Connection")
oConn.Open Application("strConnectstring")
Set rs = Server.CreateObject ("ADODB.Recordset")
rs.ActiveConnection = oConn
SQL = "Publications_PicoSearchListing"
set rs = oConn.execute(SQL)

I'm not sure if I'm doing forwardCursor or dynamic cursors, or if the provider even supports the RecordCount property. How do I check if the provider supports RecordCount property or if I'm using either forwardCursor or dynamic cursors.

Any help would be appreciated.

Thank You

+2  A: 

Please note: unless you move to the end of the recordset there is no guarantee that the RecordCount will have been populated. The standard pattern to to iterate over each row in the recordset using While Not rs.EOF. In all the VBA code I've ever written, I have never relied on checking rs.RecordCount

Rather than checking the cursor type, you can set it. For example:

Set conn=Server.CreateObject("ADODB.Connection")
conn.Provider="Microsoft.Jet.OLEDB.4.0"
conn.Open(Server.Mappath("northwind.mdb"))
set rs = Server.CreateObject("ADODB.recordset")
sql="SELECT * FROM Customers"

rs.CursorLocation = adUseClient
rs.CursorType = adOpenStatic
rs.LockType = adLockBatchOptimistic

rs.Open sql, conn

If all you want is the count, why not emit a "SELECT Count(*) From Publications_PicoSearchListing"

Of Interest?: Understanding ADO's Default Cursor Type

Another alternative to get the RecordCount is to execute:

rs.MoveLast
rs.MoveFirst

and then check the RecordCount, and even then I seem to remember some cursor types aren't guaranteed (but memory hazy on this).

Also note: Don't use the MoveLast/MoveFirst unless you really need to: this will be slow with a large recordset or a recordset drawn across a network. Instead use the Count(*) technique.

Mitch Wheat
The reason why I need to use the RecordCount is because I implementing a paging solution.
Brandon Michael Hunter
Don't use rs.MoveLast and rs.MoveFirst, this can lead to very bad performance. Use the select count(*) solution to know how many records you are dealing with.
Edelcom
A: 

Recordcount is not supported with the default forward-only cursor. you must add extra parameters to the open command

rs.open sql,conn,1,1

That should let you have access to rs.recordcount.

But paging is best done by using the Recordset.GetRows() + Recordset.Move() method.

http://databases.aspfaq.com/database/how-do-i-page-through-a-recordset.html (scroll down to the bold "Recordset.GetRows() + Recordset.Move()" this is fastest way without using stored procedures)

Drew
A: 

For paging you can use the recordset.PageSize and recordset.AbsolutePage like this

Set rs = Server.CreateObject("ADODB.Recordset")
' make recordset use adUSEclient ( client side cursor)'
rs.CursorLocation = 3
' make recordset use the adOpenStatic cursor ( scrollable )'
rs.CursorType = 3
rs.PageSize = RecordsPerPage

rs.Open sql, conn
' go to selected page'
if not rs.EOF and not rs.BOF then
    rs.AbsolutePage = page_you_want_to_go
end if

you then have access to recordset.PageCount to know the number of pages returned..

Gaby
Doesn't this read all records upto the page you are requesting ? Because you are using the records with the adUseCLient as CursorLocation.
Edelcom
actually it reads all records the fit the sql query .. but sets the current record to point at the one you want based on pagesize and absolutepage..haven't found a way to return only the data you want, allow the recordset to be disconnected and also maintain the field names ..
Gaby