I have set up a connection to my SQL server to the database where the stored procedure is located. The Stored Procedure works fine in SQLServer. The Stored Procedure runs
1 – expose parametars in excel
2 – define that parameters are entered from excel
3 – OK and refresh.
OK, You don't have to enter parameters in the command text, you can define command text in a way to expect parameters to be passed to the store procedure from excel cell. This is useful especially if your country don’t use US data format. Also, it enables end user to make refresh with some other parameters without need to edit command text. It is simple – in the excel that have connection to sql server in command text after store procedures name you enter ? – as many ? as you have different parameters that your store procedure expects (divided by ,) – like this - execute dbo.your_procedure ?,? After that, you go in the parameters tab (on the same form that you entered command text) and define from witch cell witch parameter is passed to store procedure. Of course, in the stored procedure also need to be specified what parameters are expected:
CREATE procedure [dbo].[your_procedure]
( @DateFrom datetime, @DateTo datetime ) As
--- your store procedure ---
In excel – parameter 1 will be send to the store procedure to a DateFrom parametar.
You're missing that comma
"EXECUTE dbo.GetBillingHeatMap '" & Range("A9").Value & "','" & Range("B9").Value & "'"
When you execute a stored procedure, the statement generally looks like:
EXECUTE myProc('param1', 'param2')
The way your command will expand will come out like:
EXECUTE myProc 'param1param2`
Which is nonsense. Try instead:
With ActiveWorkbook.Connections("CARLA-PC-Billing-SP").OLEDBConnection.CommandText = "EXECUTE dbo.GetBillingHeatMap ('" & Range("A9").Value & "','" & Range("B9").Value & "');"
You may still run into issues with the date formatting, so you can handle that in VBA too:
With ActiveWorkbook.Connections("CARLA-PC-Billing-SP").OLEDBConnection.CommandText = "EXECUTE dbo.GetBillingHeatMap ('" & FORMAT(Range("A9").Value, "m/d/yyyy") & "','" & FORMAT(Range("B9").Value, "m/d/yyyy") & "');"
Lastly, I find it good practice to send dynamically generated sql to a variable, and print it to the debug/immediate window before executing so I can catch stuff like that.
sqlStatement = "EXECUTE dbo.GetBillingHeatMap ('" & FORMAT(Range("A9").Value, "m/d/yyyy") & "','" & FORMAT(Range("B9").Value, "m/d/yyyy") & "');"
debug.print sqlStatement
With ActiveWorkbook.Connections("CARLA-PC-Billing-SP").OLEDBConnection.CommandText = sqlStatement
And now you'll have your statement in your immediate window, which you can copy and paste into a SQL client to execute independently of your code.
Sub Samp()
Public con As New ADODB.Connection
Public PassRecordset As New ADODB.Recordset
Public objCmd As New ADODB.Command
Dim Para1 As ADODB.Parameter
Dim Para2 As ADODB.Parameter
Dim Para3 As ADODB.Parameter
'Check the database connection
If con.State = 0 Then If Not CreateConnection
Then Exit Sub
Set objCmd = New ADODB.Command
objCmd.ActiveConnection = con
With objCmd
.ActiveConnection = con
.CommandType = adCmdStoredProc
.CommandText = StoredProcedureName
End With
Set Para1 = objCmd.CreateParameter("@TextPara1", adVarChar, adParamInput, 50, "Value1")
objCmd.Parameters.Append Para1
Set Para2 = objCmd.CreateParameter("@TextPara2", adVarChar, adParamInput, 255, "Value2")
objCmd.Parameters.Append Para2
Set Para3 = objCmd.CreateParameter("@DatePara3", adDate, adParamInput, , "DateValue")
objCmd.Parameters.Append Para3
Set PassRecordset = objCmd.Execute
Sheet1.Range("A1").CopyFromRecordset PassRecordset
End Sub