hearthhrob4gals
Board Regular
- Joined
- Aug 20, 2014
- Messages
- 142
Hi,
I'm firing a sql stored procedure from excel. Just struggling to pass the variables (dates). any help on this would be appreciated.
Regards,
Yousuf
I'm firing a sql stored procedure from excel. Just struggling to pass the variables (dates). any help on this would be appreciated.
VBA Code:
Sub ConnectSqlServer()
Dim conn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim sConnString As String
' Create the connection string.
sConnString = "Provider=SQLOLEDB;Data Source=rpasql01;" & _
"Initial Catalog=RPA_PROD;" & _
"Integrated Security=SSPI;"
' Create the Connection and Recordset objects.
Set conn = New ADODB.Connection
Set rs = New ADODB.Recordset
' Open the connection and execute.
conn.Open sConnString
Set rs = conn.Execute("[dbo].[usp_GetSummaryReport_SFS_Combined_ByDate] '2021-01-28','2021-01-29'")
' Check we have data.
If Not rs.EOF Then
' Transfer result.
Sheets("Output").Range("A1").CopyFromRecordset rs
' Close the recordset
rs.Close
Else
MsgBox "Error: No records returned.", vbCritical
End If
' Clean up
If CBool(conn.State And adStateOpen) Then conn.Close
Set conn = Nothing
Set rs = Nothing
End Sub
Regards,
Yousuf