2014-05-11 60 views
0

我试图在vba下运行,但不幸的是,当我添加参数范围其不工作(没有任何错误没有做任何事情),但如果我用简单的选择代码它的工作完美..请帮助!运行SQL存储过程从Excel VBA使用EXcel参数

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 
+0

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

回答

1

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

+0

添加“后”,其不起作用.. – user3625233

+0

您可以将您的查询设置为某个单元格以查看字符串连接的结果,并可能尝试使用Managment Studio执行它 –

相关问题