VBA 连接到 MySQL 数据库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15391014/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 20:01:44  来源:igfitidea点击:

VBA connect to MySQL DB

mysqlvbawamp

提问by Henri

I try to connect my Excel Spreadsheet to my MySQL DB hosted locally for the moment. I'm using WAMPSERVER.

我尝试将我的 Excel 电子表格连接到我目前在本地托管的 MySQL 数据库。我正在使用 WAMPSERVER。

Here is my VBA code :

这是我的 VBA 代码:

Dim oConn As ADODB.Connection
Private Sub ConnectDB()
    Set oConn = New ADODB.Connection
    oConn.Open "DRIVER={MySQL ODBC 5.1 Driver};" & _
        "SERVER=localhost;" & _
        "DATABASE=test;" & _
        "USER=root;" & _
        "PASSWORD=;" & _
        "Option=3"
End Sub

I created my db "test" on through phpMyadmin... I have an error when I run the code. Do you have an idea?

我通过 phpMyadmin 创建了我的数据库“测试”......运行代码时出现错误。你有想法吗?

采纳答案by Henri

The problem was due to a wrong references defined. In the VBE, I had to click on Tools>References and check the "Microsoft ActiveX Data Objects 6.1 Library", and only this one.

问题是由于定义了错误的引用。在 VBE 中,我必须单击工具>参考并检查“Microsoft ActiveX 数据对象 6.1 库”,并且只有这一项。

The macro is properly running now under WAMP (with default parameters Username = root and Pwd = "") on Windows 7 with the following code :

该宏现在在 Windows 7 上的 WAMP 下正常运行(使用默认参数 Username = root 和 Pwd = ""),代码如下:

Dim oConn As ADODB.Connection
Private Sub ConnectDB()
    Set oConn = New ADODB.Connection
    oConn.Open "DRIVER={MySQL ODBC 3.51 Driver};" & _
        "SERVER=localhost;" & _
        "DATABASE=excel;" & _
        "USER=root;" & _
        "PASSWORD=;" & _
        "Option=3"
End Sub