Hello all,
I'm a VB newbie and I've been trying to piece together snippets of code in order to call a SQL Stored Procedure and return the result-set to an excel sheet.
I'm (mis)using VBA and ADO, and I've been able to run a sinple select statement, but I've gotten no-where trying to run a SP.
With the code I have supplied, my trying to pass 2 parameters.
The error message being returned is:
Runtime Error '3265';
Item cannot be found in the collection corresponding to the requested name or ordinal
The code I have at the moment is:
Any tips/pointers/help will be gratefully recieved! Thankyou in advance folks!
Kind Regards,
Casey.
I'm a VB newbie and I've been trying to piece together snippets of code in order to call a SQL Stored Procedure and return the result-set to an excel sheet.
I'm (mis)using VBA and ADO, and I've been able to run a sinple select statement, but I've gotten no-where trying to run a SP.
With the code I have supplied, my trying to pass 2 parameters.
The error message being returned is:
Runtime Error '3265';
Item cannot be found in the collection corresponding to the requested name or ordinal
The code I have at the moment is:
Code:
Public Sub test()
'Create a connection object
Dim conPayG As ADODB.Connection
Set conPayG = New ADODB.Connection
'Provide a connection string
Dim strConPayG As String
'Use the SQL Server OLE DB Provider
strConPayG = "PROVIDER=SQLOLEDB;"
'Connect to the PayGlobal DB on the MOF-CH-SQL server
strConPayG = strConPayG & "DATA SOURCE=mof-ch-sql;INITIAL CATALOG=payglobal;"
'Use an integrated login
strConPayG = strConPayG & " INTEGRATED SECURITY=sspi;"
'Now open the connection
conPayG.Open strConPayG
'*******************************************
'Create a command object
Dim cmdSP As ADODB.Command
Set cmdSP = New ADODB.Command
'Set the text of the command sent to SQL Server
cmdSP.CommandText = "dbo.test"
'Identify the type of command
cmdSP.CommandType = adCmdStoredProc
'Automatically fill-in parameter info from the stored procedure
cmdSP.Parameters.Refresh
'Set parameter 1
cmdSP(1) = "31/10/2004"
'Set parameter 2
cmdSP(2) = "1/12/2004"
'*******************************************
'Create a recordset object
Dim rsSP As ADODB.Recordset
Set rsSP = New ADODB.Recordset
Set rsSP = cmdSP.Execute()
With rsSP
'Resultset > Sheet 1 cell A1
Sheet1.Range("A1").CopyFromRecordset rsSP
'Tidy up
.Close
End With
End Sub
Any tips/pointers/help will be gratefully recieved! Thankyou in advance folks!
Kind Regards,
Casey.