【问题标题】:Running SQL Stored Procedure From Excel VBA Using Parameters From EXcel使用 EXcel 中的参数从 Excel VBA 运行 SQL 存储过程
【发布时间】:2014-05-11 10:16:13
【问题描述】:

我正在尝试在 vba 下运行,但不幸的是,当我添加参数范围时,它不起作用(什么都不做,没有任何错误),但如果我使用简单选择的代码,它可以完美工作..请帮助!

    Dim HRCONN As ADODB.Connection
    Dim HRDATA As ADODB.Recordset
    Dim HRFIELD As ADODB.FIELD

    Set HRCONN = New ADODB.Connection
    Set HRDATA = New ADODB.Recordset

    HRCONN.connectionstring = CONSTRSQL
    HRCONN.Open

    On Error GoTo closeconnection

    With HRDATA
    .ACTIVECONNECTION = HRCONN

    .Source = "EXEC DBO.LATENCY_REPORT" & "'" & Range("M12").Value & "'" & "," & "'" & Range("M13").Value & "'" & "," & "'" & Range("B12").Value & "'"""


    .locktype = adLockReadOnly
    .cursortype = adopenforwardonly
    .Open
    End With

    On Error GoTo closerecordset

    Worksheets.Add

    For Each HRFIELD In HRDATA.Fields
        ActiveCell.Value = HRFIELD.Name
        ActiveCell.Offset(0, 1).Select
    Next HRFIELD

    Range("A1").Select
    Range("A2").CopyFromRecordset HRDATA
    Range("A1").CurrentRegion.EntireColumn.AutoFit

    On Error GoTo 0
closerecordset:
    HRDATA.Close
closeconnection:
    HRCONN.Close
End Sub

【问题讨论】:

  • 你能告诉我你在代码中添加参数的位置吗?

标签: sql sql-server vba stored-procedures excel-2013


【解决方案1】:

我从来没有用 VBA 做过这样的事情,但我用 UI 做过。所以,首先我要做的是查询类型。第二,我没有看到任何参数之间的“,”。

【讨论】:

  • 您可以将查询设置到某个单元格以查看字符串连接的结果,并可以尝试使用 Managment Studio 执行它
猜你喜欢
  • 1970-01-01
  • 2018-09-02
  • 1970-01-01
  • 1970-01-01
  • 2019-06-25
  • 1970-01-01
  • 1970-01-01
  • 2018-11-10
  • 1970-01-01
相关资源
最近更新 更多