Question : Stored Procedure Fails To Populate Recordset

Hello,

I have a MS Access 2K3 FE application with a SQL 2K5 BE.  I am trying to run a stored procedure to populate a recordset.  Everything seems to run fine, but the recordset has a RowCount of -1 after the procdure executes.  I receive no error messages whatsoever, and permissions are set up correctly.  The stored procedure executes fine in the Mgmt Console on SQL Server.

I just need another set of eyes on this, the code looks pretty sound to me.

Thanks,
-Torrwin
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
'Code to run the stored procedure'
    Dim myCommand As ADODB.Command
    Set myCommand = New ADODB.Command
    
    Dim rsTemp As ADODB.Recordset
    Set rsTemp = New ADODB.Recordset
    
    Dim Param1 As ADODB.Parameter
    
    '---Code to open connection here, the connection works fine.'
        
    myCommand.ActiveConnection = m_oConn
    myCommand.CommandText = "usp_Test"
    myCommand.CommandType = adCmdStoredProc

    Set Param1 = myCommand.CreateParameter("Param1", adVarChar, adParamInput, Len("Test"), "Test")
    myCommand.Parameters.Append Param1
    
    'Populate the recordset'
    Set rsTemp = myCommand.Execute

    'rsTemp.Open myCommand   This statement yields the same results'

Answer : Stored Procedure Fails To Populate Recordset

You're opening an ADO Recordset using the default Cursor and Locktype. The RowCount will ALWAYS be -1 in that scenario. If you want a true RowCount/RecordCount, you must change the Cursor and Locktype according. Also, here's a simpler method of doing that:

Dim rst As ADODB.Recordset

Set rst = New ADODB.Recordset
rst.Open "EXEC usp_Text @Param1='Test', m_oConn, adOpenKeyset,adLockOptimistic

Your rst variable would then contain the fully populated recordset, and would contain a valid RecordCount. That said, unless you have a good reason for opening a Recordset with these parameters, then you're much better off using Count(*) or something of that nature to get a valid Recordcount ... using OpenKeyset or OpenDynamic is normally a poor choice, performance-wise
   
Random Solutions  
 
programming4us programming4us