У меня есть таблица Excel Spreed, в которой нет данных, возвращаемых из сохраненного процесса. Если я запускаю созданный sql, он работает нормально.
Private Sub Refresh_Click()
On Error GoTo eh:
Dim DateFrom As Date 'Declare the SellStartDate as Date
Dim DateTo As Date 'Declare the SellEndDate as Date
SellStartDate = Sheets("Sheet1").Range("B2").Value
SellEndDate = Sheets("Sheet1").Range("B3").Value
SellStartDate = Format(Sheets("Sheet1").Range("B2").Value2, "yyyy-mm-dd")
SellEndDate = Format(Sheets("Sheet1").Range("B3").Value2, "yyyy-mm-dd")
Dim sql As String
sql = "EXECUTE [dbo].[fsp_PLReportByDates] @DateFrom = '" & SellStartDate & "', @DateTo = '" & SellEndDate & "'"
'Pass the Parameters values to the Stored Procedure used in the Data Connection
With ActiveWorkbook.Connections("HertShineConnection").OLEDBConnection.CommandText = sql
ActiveWorkbook.Connections("HertShineConnection").Refresh
Done:
Exit Sub
eh:
MsgBox "The following error occurred: " & Err.Description
End With
End Sub
Однако, когда я выполняю вышеупомянутое, он не работает, когда я отлаживаю, получают правильные даты.
![enter image description here](https://i.stack.imgur.com/VdjQp.png)
Как вы видите, когда я шагаю по коду, его просто добавляют? не фактические данные, какие-либо идеи.
Редактировать 1
С тех пор я получил код, открывающий соединение, но когда я иду, чтобы выполнить сохраненный процесс, он все еще терпит неудачу.
Private Sub GetData()
Dim con As ADODB.Connection
Dim cmd As ADODB.Command
Dim rs As ADODB.Recordset
Dim WSP1 As Worksheet
Set con = New ADODB.Connection
Set cmd = New ADODB.Command
Set rs = New ADODB.Recordset
Application.DisplayStatusBar = True
Application.StatusBar = "Contacting SQL Server..."
' Remove any values in the cells where we want to put our Stored Procedure's results.
Dim rngRange As Range
Set rngRange = Range(Cells(8, 2), Cells(Rows.Count, 1)).EntireRow
rngRange.ClearContents
Dim Server_Name As String
Dim Database_Name As String
Dim User_ID As String
Dim Password As String
Dim SQLStr As String
SellStartDate = Sheets("Sheet1").Range("B2").Value
SellEndDate = Sheets("Sheet1").Range("B3").Value
Server_Name = "server" ' Enter your server name here
Database_Name = "db" ' Enter your database name here
User_ID = "sa" ' enter your user ID here
Password = "password" ' Enter your password here
con.Open "Driver={SQL Server};Server=" & Server_Name & ";Database=" & Database_Name & _
";Uid=" & User_ID & ";Pwd=" & Password & ";"
With cmd
.ActiveConnection = con
.CommandText = "mystoreproc"
.CommandType = adCmdStoredProc
.Parameters.Refresh
.Parameters.Append cmd.CreateParameter("@DateFrom", adDate, adParamInput)
.Parameters.Append cmd.CreateParameter("@DateTo", adDate, adParamInput)
.Parameters.Item("@DateFrom").Value = SellStartDate
.Parameters.Item("@DateTo").Value = SellEndDate
strSql = cmd.CommandText
End With
Set rs = cmd.Execute
Application.StatusBar = "Running stored procedure..."
Set rs = Nothing
Set cmd = Nothing
con.Close
Set con = Nothing
![enter image description here](https://i.stack.imgur.com/ePJCd.png)