Ado Recordset and Record Count Urgent !!!!

Posted on 2000-04-19
Medium Priority
Last Modified: 2006-11-17
I am trying to execute a stored procedure with one parameter.If i use the recordset.open method i get the record count as 11 and if i use the command object i get the record count as -1

here is the code for the command object

 Set oConn = New ADODB.Connection
strConnstring = "DSN=Bull;UID=ram;PWD=rajesh"
oConn.Open strConnstring, ram, rajesh, -1

'Create the ADO Database Connection:
Set oComm = New ADODB.Command
Set oPr = New ADODB.Parameter

Set oComm.ActiveConnection = oConn
oComm.CommandText = "ccc_statementofwork"
oComm.CommandType = adCmdStoredProc
oComm.CommandTimeout = 30000
oPr.Type = adInteger
oPr.Value = 134325
oComm.Parameters.Append oPr
Set recBull = New ADODB.Recordset
recBull.CursorLocation = adUseServer
recBull.CursorType = adOpenDynamic
recBull.LockType = adLockBatchOptimistic
Set recBull = oComm.Execute()

The same stored procedure when used in an asp page using the recordset.open method gives the record count as -1

I really need this quite urgently

Any help will be really appreciated

Question by:sramkris
LVL 14

Accepted Solution

wsh2 earned 225 total points
ID: 2731823
recBull.CursorType = adOpenDynamic
From MSDN (Please Note the last paragraph).. <smile>:
RecordCount Property

Indicates the current number of records in a Recordset object.

Return Value:
Returns a Long value.

Use the RecordCount property to find out how many records are in a Recordset object. The property returns -1 when ADO cannot determine the number of records or if the provider or cursor type does not support RecordCount. Reading the RecordCount property on a closed Recordset causes an error.

If the Recordset object supports approximate positioning or bookmarks—that is, Supports (adApproxPosition) or Supports (adBookmark), respectively, return True—this value will be the exact number of records in the Recordset regardless of whether it has been fully populated. If the Recordset object does not support approximate positioning, this property may be a significant drain on resources because all records will have to be retrieved and counted to return an accurate RecordCount value.

The cursor type of the Recordset object affects whether the number of records can be determined. The RecordCount property will return -1 for a forward-only cursor, the actual count for a static or keyset cursor, and either -1 or the actual count, depending on the data source, for a dynamic cursor.

LVL 14

Expert Comment

ID: 2731830
Change your CursorType -or- execute a separate SELECT COUNT SQL statement to get what you want.. <smile>.

Author Comment

ID: 2731860
My cursor type is adopendynamic.The problem still persist.Is it because i use Server side objects.The asp page where is have the code
Set oConn = Server.CreateObject("ADODB.Connection")
strConnstring = "DSN=Bull;UID=ram;PWD=rajesh"
oConn.Open strConnstring, ram, rajesh, -1
oConn.CommandTimeout = 30000
set rsIni = server.CreateObject ("Adodb.recordset")
      Set rsIni.ActiveConnection = oConn
        rsIni.Open "ccc_statementofwork " & 134325,,adOpenKeyset,adLockBatchOptimistic  
gives the same error.If the cursor type is adopenkeyset or adopendynamic the result is the same

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

LVL 14

Expert Comment

ID: 2731895
rsIni.Open "ccc_statementofwork " & 134325,,adOpenKeyset,adLockBatchOptimistic    

"ccc_statementofwork " & 134325
Are you trying to pass a parameter here? You have to put Command Parameters into the Parameter collection to do so.. I think your -1 is because you are erroring out.. <smile>.  
LVL 14

Expert Comment

ID: 2731913
From MSDN:

Parameters Collection

Contains all the Parameter objects of a Command object.


A Command object has a Parameters collection made up of Parameter objects.

Using the Refresh method on a Command object's Parameters collection retrieves provider parameter information for the stored procedure or parameterized query specified in the Command object. Some providers do not support stored procedure calls or parameterized queries; calling the Refresh method on the Parameters collection when using such a provider will return an error.

If you have not defined your own Parameter objects and you access the Parameters collection before calling the Refresh method, ADO will automatically call the method and populate the collection for you.

You can minimize calls to the provider to improve performance if you know the properties of the parameters associated with the stored procedure or parameterized query you wish to call. Use the CreateParameter method to create Parameter objects with the appropriate property settings and use the Append method to add them to the Parameters collection. This lets you set and return parameter values without having to call the provider for the parameter information. If you are writing to a provider that does not supply parameter information, you must manually populate the Parameters collection using this method to be able to use parameters at all. Use the Delete method to remove Parameter objects from the Parameters collection if necessary.

Parameter Object

A Parameter object represents a parameter or argument associated with a Command object based on a parameterized query or stored procedure.


Many providers support parameterized commands. These are commands where the desired action is defined once, but variables (or parameters) are used to alter some details of the command. For example, an SQL SELECT statement could use a parameter to define the matching criteria of a WHERE clause, and another to define the column name for a SORT BY clause.

Parameter objects represent parameters associated with parameterized queries, or the in/out arguments and the return values of stored procedures. Depending on the functionality of the provider, some collections, methods, or properties of a Parameter object may not be available.

With the collections, methods, and properties of a Parameter object, you can do the following:

Set or return the name of a parameter with the Name property.

Set or return the value of a parameter with the Value property.

Set or return parameter characteristics with the Attributes and Direction, Precision, NumericScale, Size, and Type properties.

Pass long binary or character data to a parameter with the AppendChunk method.
If you know the names and properties of the parameters associated with the stored procedure or parameterized query you wish to call, you can use the CreateParameter method to create Parameter objects with the appropriate property settings and use the Append method to add them to the Parameters collection. This lets you set and return parameter values without having to call the Refresh method on the Parameters collection to retrieve the parameter information from the provider, a potentially resource-intensive operation.

LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 2732130
Dear sramkris, i think you were so close to your solution:
Set recBull = oComm.Execute()
recBull.Opne oComm

Your statement erases the recordset you defined beforehand, and the command object always returns a ForwardOnly recordset (including that Recordcount will be -1 unless you did a Movelast, but then you can't go back :-(

hope this helps

Expert Comment

ID: 2735034
You can try with

oConn.Open strConnstring, ram, 3,3


Expert Comment

ID: 2740175

An ADO recordset is unable to provide a definite value for Recordcount when using dynamic cursors.Try Changeing your cursor type :

recBull.CursorLocation = adUseClient

Having said that, you do not always get the cursor you asked for.If you asked for certain kind of cursor combination ,which cannot be supported by your provider,ADO will give you another cursor combination that is supported by the provider without generating an error message.SO make sure you got what you asked for ,by checking the cursorType property on the Recordet after you have opened the Recordset.When you cursor get a -1 in the Recordcount that shows the cursor does not support Recordcount.SO change it to Keyset cursor instead of Dynamic cursor.This should solve you problem.Wish you Good Luck.

Author Comment

ID: 2752542
Thanks a lot guys for helping me out.This was a lot of information i received.


Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

579 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question