Question : MS Access Call to SQL Stored Proc with Return Value

I need to pass an integer value into the stored procedure, and I want an integer returned somehow.  Since I found this code example, I will go with the recordset way.  I modified my stored proc to select the integer I want to return.  There will only be one integer value returned.

I tried the code in this related question, but I get "Syntax error or access violation." on the objRecordset.Open line.  I added SET NOCOUNT ON to my stored procedure and that did not help.  

Please advise.  Thank you!
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
Dim i As Integer
    
    Dim strConnectionString As String
        strConnectionString = "Provider=SQLOLEDB; Data Source=127.0.0.1; Initial Catalog=Process; Integrated Security=SSPI;"
    Dim strSql As String
        'Stored Procedure:
        strSql = "NewProcessFromOld_sp " & lstRecordList.Value
    Dim objDatabase As ADODB.Connection
    Dim objRecordset As ADODB.Recordset
    Dim objCmd As ADODB.Command
    
    Set objDatabase = New ADODB.Connection
        objDatabase.Open (strConnectionString)
    
    Set objCmd = New ADODB.Command
        With objCmd
            .CommandText = strSql
            .CommandType = adCmdStoredProc
            .ActiveConnection = objDatabase
        End With
        
    Set objRecordset = New ADODB.Recordset
        objRecordset.Open objCmd, , adOpenStatic, , adCmdStoredProc
 
            Do While Not objRecordset.EOF
                    i = objRecordset.Fields("NewID").Value
                objRecordset.MoveNext
            Loop
        objRecordset.Close
    
    objDatabase.Close

Answer : MS Access Call to SQL Stored Proc with Return Value

You may be able to simplify this, since you're not really using the command object for anything, and your Stored Proc only returns a single value:

Dim rst As ADODB.Recordset
Dim con As ADODB.Connection

Set con = New ADODB.Connection
con.OPen strConnectionString

Set rst = New ADODB.Recordset
rst.Open "EXEC YourStoredProcName", con

If Not (rst.EOF and rst.BOF) Then
  Else
  Msgbox "No Records Returned"
End If
Random Solutions  
 
programming4us programming4us