Sql server 在Excel消息框中显示SQL Server过程调用的结果

Sql server 在Excel消息框中显示SQL Server过程调用的结果,sql-server,vba,excel,stored-procedures,Sql Server,Vba,Excel,Stored Procedures,我在Excel中有一个VBA sub。它需要调用SQL Server过程,并在消息框中显示调用结果。(主要是因为我在维护别人的代码——否则,我就不会使用Excel。) 我创建SQL语句来调用该过程。我有打开连接的潜艇。但在显示过程调用结果的过程中,我仍然缺少一些东西 以下是我目前掌握的情况: Dim Today As Date Dim result As Date Dim sSQLStatement As String Dim strDate As String Dim Recordset As

我在Excel中有一个VBA sub。它需要调用SQL Server过程,并在消息框中显示调用结果。(主要是因为我在维护别人的代码——否则,我就不会使用Excel。)

我创建SQL语句来调用该过程。我有打开连接的潜艇。但在显示过程调用结果的过程中,我仍然缺少一些东西

以下是我目前掌握的情况:

Dim Today As Date
Dim result As Date
Dim sSQLStatement As String
Dim strDate As String
Dim Recordset As ADODB.Recordset

Today = DateTime.Now()

result = DateSerial(Year(Today), Month(Today) - 1, 1)
strDate = Year(result) & "-" & Format(Month(result), "00") & "-" & Format(Day(result), "00")

DBOpen

sSQLStatement = "set nocount on; EXEC [MySchema].[dbo].[MyProcedure] @END_DATE = N'" & strDate & "'"

MsgBox sSQLStatement

Dim i As Long
Dim Ary
Dim strMsg As String

Set Recordset = New ADODB.Recordset
With Recordset
    .ActiveConnection = cnPubs 'this is defined elsewhere, used in the DBOpen call, and works

    'I can call the execute and it works, but it fails here when I try to assign the results to the recordset
     Recordset = cnPubs.Execute(sSQLStatement)

     'I found this online and don't know yet if it works.  I have to get past the statement above first.
     If Not Recordset.EOF Then
          Ary = Recordset.GetRows
          For i = 0 To UBound(Ary, 2)
              If i = 0 Then
                  strMsg = Ary(0, i)
              Else
                  strMsg = strMsg & ", " & Ary(0, i)
              End If
          Next i
          MsgBox strMsg, , UBound(Ary, 2) + 1 & " records"
     End If
     .Close
End With

DBClose
因此,如果我单独调用cnPubs.Execute(sSQLStatement),我可以判断它正在运行。我打开和关闭连接都很好。但它应该返回一组几行,我需要显示它。我不想把它们写到电子表格的任何地方——它们应该显示在一个消息框中。

更改

Recordset = cnPubs.Execute(sSQLStatement)

改变


如果在它调用GetRows之后在行上放置一个断点,那么有多少条记录?没有那么远。尽管如此,如果我从上面的行中删除“Recordset=”。。。我将尝试一下。在recordset=行上放置一个断点,并告诉我sSQLStatement变量说了什么(您可以将它打印到调试窗口)。如果在它调用GetRows之后立即在该行上放置一个断点,那么有多少条记录?没有那么远。尽管如此,如果我从上面的行中删除“Recordset=”。。。我将尝试一下。在recordset=行上放置一个断点,并告诉我sSQLStatement变量的内容(您可以将其打印到调试窗口)谢谢,谢谢,谢谢!我现在需要更好地格式化输出,但现在我有了输出,这很简单。谢谢,谢谢,谢谢!我现在需要比现有的更好地格式化输出,但现在我有了输出,这很简单。
recordset.open sSQLStatement, cnPubs