I am trying to run a stored procedure from Excel using VBA, but I'm getting a "Type Mismatch" error with the following code. Can I execute a Stored procedure with parameters as a string passed to the Command object as shown below?
Function Sproc()
Dim cnn As New ADODB.Connection
Dim rst As New ADODB.Recordset
Dim cmd As ADODB.Command
Dim ConnectionString As String
Dim StrSproc As String
ConnectionString = "Provider=SQLOLEDB;Data Source=DBSource;" & _
"Initial Catalog=CurrentDb;" & _
"Integrated Security=SSPI;"
'Opens connection to the database
cnn.Open ConnectionString
'Timeout error in seconds for executing the entire query; this will run for 15 minutes
'before VBA times out, but your database might timeout before this value
cnn.CommandTimeout = 900
StrSproc = "EXEC StoredProcedure " & _
"@parameter1 = 0," & _
"@parameter2 = 0," & _
"@parameter3 = 0,"
Application.StatusBar = "Running stored procedure..."
Set rst = cmd.Execute(, , StrSproc)
End Function