Send date value from Excel VBA to SQL Server Stored Procedure

Xephire

New Member
Joined
Oct 17, 2008
Messages
13
All,

We try to extract data from a SQL Server 2008 R2 database using a stored procedure into an Excel 2010 workbook. The parameters (client code and end date) are populated by the client at runtime, and used as parameters for the stored procedure.

The problem is following.
When we send the date parameter value as a date, it does not have the quotes around it, and SQL Server cannot parse it. (e.g. 2012-08-29)
When we send the date parameter value as a date and add quotes ("'") around the date, it is no longer seen as a date but as a string, and VBA gives an error (e.g. '2012-08-09')
When we change type to string, the stored procedure does not accept the value, as it is a string, not a date.

Does anyone have expericence with parsing a date parameter to a SQL server stored procedure?

*** VBA sniplet of the code ***
'Execute the stored procedure once & store the output on the Main Data Tab.</SPAN></SPAN>
Set objRs = New ADODB.Recordset</SPAN></SPAN>
Set objCmd = New ADODB.Command</SPAN></SPAN>

With objCmd</SPAN></SPAN>
.ActiveConnection = objDBConn</SPAN></SPAN>
.CommandType = adCmdStoredProc</SPAN></SPAN>
.CommandText = "XXX" ' Stored procedure</SPAN></SPAN>

'Return value parameter to check SP executed sucessfuly of not</SPAN></SPAN>
.Parameters.Append .CreateParameter("@RETURNVALUE", adInteger, adParamReturnValue)</SPAN></SPAN>

' Actual Parameters</SPAN></SPAN>
.Parameters.Append .CreateParameter("@ClientCode", adVarChar, adParamInput, 2400)</SPAN></SPAN>
.Parameters.Append .CreateParameter("@EndDate", adDate, adParamInput, 12)</SPAN></SPAN>

'Parameter Values</SPAN></SPAN>
.Parameters("@ClientCode").Value = "'" & ClientCode & "'"</SPAN></SPAN>
.Parameters("@EndDate").Value = CDate(strEndDate)</SPAN></SPAN>

End With</SPAN></SPAN>

Set objRs = objCmd.Execute()</SPAN></SPAN>

lRetVal = objCmd.Parameters("@RETURNVALUE").Value</SPAN></SPAN>

If lRetVal = 0 Then</SPAN></SPAN>
MsgBox "Stored procedure executed sucessfully", vbInformation</SPAN></SPAN>
Else</SPAN></SPAN>
MsgBox "Stored procedure execution Fail", vbInformation</SPAN></SPAN>
Exit Sub</SPAN></SPAN>
End If</SPAN></SPAN>
 

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top