Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to Use RecordCount Method

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
Hi,

I try to count record number in a recordset. I open my connection with cursor type 1 and lock type 3:

Set conn = Server.CreateObject("ADODB.Connection")
Conn.Open "DSN=name", 1,3

And I use the RecordCount method:

count = rs.RecordCount

But I always get -1 for the count.

Can anyone figure out the problem for me?

Thank you.

 
ShadowC,

I don't know much about it but try specifying the Cursor Location as adUseClient.

<%
Dim rs
Dim count

rs = Server.CreateObject(&quot;ADODB.Recordset&quot;)
rs.CursorLocation = 3
rs.open &quot;DSN=name&quot;, 1,3

count = rs.RecordCount
%>

Mise Le Meas,

Mighty :)
 
That means that it can't be determined (probably not supported by the properties you have selected)

Should be the cursorType that is giving you the headache... Try a 3 (adOpenStatic) -- If that doesn't work, continue trying others till it does. ;-)

Paul Prewett
 
I tried every combination and none of them works. Should I change something at the SQL Server?
 
From '

Q2) I keep on getting a &quot;-1&quot; for the my Recordset's RecordCount property. Why?

By default, ADO creates a server-side recordset with a CursorType of forward-only (adOpenForwardOnly). A forward-only cursor does not support the RecordCount property, even if you move to the last record in the recordset after you've opened it.

If you need the RecordCount to be correct, then set the CursorType to something other than forward-only (e.g. adOpenKeyset or adOpenStatic). Using a dynamic cursor (adOpenDynamic) will not help in this case since the number of records in a dynamic cursor may change.

If you create a client-side recordset (adUseClient), then ADO automatically sets the CursorType to a static cursor (adOpenStatic), and hence the RecordCount property will always be correct for &quot;client-side&quot; recordsets.

For more info, see Microsoft KB Article Q194973

Yours,

Rob.
 
I added rs.CursorLocation = adUseClient and it works now.

Thank you all, guys.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top