VBA 连接到 MySQL 数据库 [英] VBA connect to MySQL DB

查看:89
本文介绍了VBA 连接到 MySQL 数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

这是我的 VBA 代码:

Here is my VBA code :

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

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

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

推荐答案

问题是由于定义了错误的引用.在 VBE 中,我必须点击工具>参考并检查Microsoft ActiveX Data Objects 6.1 Library",并且只有这个.

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.

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

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

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

这篇关于VBA 连接到 MySQL 数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆