Hi,
I have written vba code to run a stored procedure and copy the result set by using the CopyResultSet from Excel VBA. I added a for loop to reference the column headers explicitly but when I check the value in the "watch" window it says there is no value in any of the iterations. Not sure how this is possible. The only thing that I can think of is that the stored proc creates a temporary table which is what excel references as the result set. The reason for the temporary table creation is because the stored proc creates a dynamic result so the table changes every time it is called. Any help would be greatly appreciated!!
Thanks in advance.
Here is the actual code:
Private Sub cmdRunExport_Click()
'create a connection object
Dim conn As ADODB.Connection
Set conn = New ADODB.Connection
'create parameter variable
Dim prm As ADODB.Parameter
'create a recordset
Dim rsResponses As ADODB.Recordset
Set rsResponses = New ADODB.Recordset
'create a command to execute stored procedure
Dim cmdSQL As ADODB.Command
Set cmdSQL = New ADODB.Command
'provide the connection string
Dim strConn As String
'Use the SQL Server OLEDB Provider
strConn = "Provider=SQLOLEDB;"
'Connect to the Database on the xxx server
strConn = strConn & "DATA SOURCE=server;INITIAL CATALOG=dbname;"
'Use an integrated login.
strConn = strConn & " User ID=xxxxx;PASSWORD=xxxxx;"
'Open connection
conn.Open strConn
With cmdSQL
.ActiveConnection = conn
.CommandText = "zzKS_USP_StoredProc"
.CommandType = adCmdStoredProc
Set prm = .CreateParameter("caseid", adInteger, adParamInput)
.Parameters.Append prm
.Parameters("caseid").Value = FrmResponseExport.txtCaseid.Value
End With
Dim fields As String
Dim i As Integer
'loop to get column headings.
For i = 1 To rsResponses.fields.Count
Worksheets("Sheet1").Cells(i, 1).Value = rsResponses.fields(i).Name
Next i
'copy the result of the stored proc to the second row of the excel sheet
Set rsResponses = cmdSQL.Execute(CInt(FrmResponseExport.txtCaseid.Value))
Worksheets("Sheet1").Range("A2").CopyFromRecordset rsResponses
rsResponses.Close
conn.Close
Set rsResponses = Nothing
Set conn = Nothing
MsgBox "All done", vbOKOnly, "Finished"
End Sub
I have written vba code to run a stored procedure and copy the result set by using the CopyResultSet from Excel VBA. I added a for loop to reference the column headers explicitly but when I check the value in the "watch" window it says there is no value in any of the iterations. Not sure how this is possible. The only thing that I can think of is that the stored proc creates a temporary table which is what excel references as the result set. The reason for the temporary table creation is because the stored proc creates a dynamic result so the table changes every time it is called. Any help would be greatly appreciated!!

Here is the actual code:
Private Sub cmdRunExport_Click()
'create a connection object
Dim conn As ADODB.Connection
Set conn = New ADODB.Connection
'create parameter variable
Dim prm As ADODB.Parameter
'create a recordset
Dim rsResponses As ADODB.Recordset
Set rsResponses = New ADODB.Recordset
'create a command to execute stored procedure
Dim cmdSQL As ADODB.Command
Set cmdSQL = New ADODB.Command
'provide the connection string
Dim strConn As String
'Use the SQL Server OLEDB Provider
strConn = "Provider=SQLOLEDB;"
'Connect to the Database on the xxx server
strConn = strConn & "DATA SOURCE=server;INITIAL CATALOG=dbname;"
'Use an integrated login.
strConn = strConn & " User ID=xxxxx;PASSWORD=xxxxx;"
'Open connection
conn.Open strConn
With cmdSQL
.ActiveConnection = conn
.CommandText = "zzKS_USP_StoredProc"
.CommandType = adCmdStoredProc
Set prm = .CreateParameter("caseid", adInteger, adParamInput)
.Parameters.Append prm
.Parameters("caseid").Value = FrmResponseExport.txtCaseid.Value
End With
Dim fields As String
Dim i As Integer
'loop to get column headings.
For i = 1 To rsResponses.fields.Count
Worksheets("Sheet1").Cells(i, 1).Value = rsResponses.fields(i).Name
Next i
'copy the result of the stored proc to the second row of the excel sheet
Set rsResponses = cmdSQL.Execute(CInt(FrmResponseExport.txtCaseid.Value))
Worksheets("Sheet1").Range("A2").CopyFromRecordset rsResponses
rsResponses.Close
conn.Close
Set rsResponses = Nothing
Set conn = Nothing
MsgBox "All done", vbOKOnly, "Finished"
End Sub