从VBA调用mySQL存储过程(Excel 2013)

时间:2015-01-29 15:37:14

标签: mysql excel vba excel-vba stored-procedures

我试图从VBA调用mySQL存储过程。存储过程在mySQl中运行良好,但我无法将其记录到excel中的目标单元格中​​。在这个简单的测试工作之后,我还想让VBA代码复制一个单元格(例如sheet4.cells(5,1))并使用复制的值作为查询的被调用过程的参数。我将不胜感激任何帮助。

Private Sub MySQL_Click()

Dim sSQL As String
Dim rs As ADODB.Recordset  
Dim cn As ADODB.Connection 
Dim cmdObj As ADODB.Command 

 Set cn = New ADODB.Connection
 cn.Open "DRIVER={MySQL ODBC 5.3 Unicode Driver};" & _
        "SERVER=192.168.0.1;" & _
        "DATABASE=enviroland;" & _
        "USER=enviro_land;" & _
        "PASSWORD=enviro_land;" & _
        "Option=3"

sSQL = "CALL test_proc();"

Set rs = New ADODB.Recordset
rs.CursorLocation = adUseClient
rs.Open sSQL, cn, adOpenForwardOnly, adLockReadOnly, adCmdText

Sheet2.Range("A1:A1").CopyFromRecordset rs

rs.Close
Set rs = Nothing
cn.Close
Set cn = Nothing

End Sub

1 个答案:

答案 0 :(得分:0)

尝试使用参数adCmdStoredProc

将命令设置为存储过程
Set rs = New ADODB.Recordset
rs.CursorLocation = adUseClient
rs.Open sSQL, cn, adOpenForwardOnly, adLockReadOnly, adCmdStoredProc