尝试使用 ODBC 连接 Excel 和 MySQL 时出现此错误
DataSource name not found and no default driver specified
这是我的 VBA 代码:
Sub test123()
' Connection variables
Dim conn As New ADODB.Connection
Dim server_name As String
Dim database_name As String
Dim user_id As String
Dim password As String
' Table action variables
Dim i As Long ' counter
Dim sqlstr As String ' SQL to perform various actions
Dim table1 As String, table2 As String
Dim field1 As String, field2 As String
Dim rs As ADODB.Recordset
Dim vtype As Variant
'----------------------------------------------------------------------
' Establish connection to the database
server_name = "127.0.0.1" ' Enter your server name here - if running from a local computer use 127.0.0.1
database_name = "smss" ' Enter your database name here
user_id = "root" ' enter your user ID here
password = "" ' Enter your password here
Set conn = New ADODB.Connection
conn.Open "DRIVER={MySQL ODBC 5.2a Driver}" _
& ";SERVER=" & server_name _
& ";DATABASE=" & database_name _
& ";UID=" & user_id _
& ";PWD=" & password _
' Extract MySQL table data to first worksheet in the workbook
GoTo skipextract
Set rs = New ADODB.Recordset
sqlstr = "SELECT * FROM inbox" ' extracts all data
rs.Open sqlstr, conn, adOpenStatic
With Sheet1(1).Cells ' Enter your sheet name and range here
.ClearContents
.CopyFromRecordset rs
End With
skipextract:
End Sub
我添加了引用(工具引用)
ODBC 驱动程序也已安装。
到底哪里出了问题?谢谢。
最佳答案
这个网站上有很多文章描述了类似的问题。特别是,在 this link 中有几个指针。听起来不错。
在你上面的代码中,有一行特别让我觉得麻烦:
Dim conn As New ADODB.Connection
跟着往下走
Set conn = New ADODB.Connection
第二个以一种让我感到不舒服的方式覆盖第一个 - 虽然我不能告诉你到底出了什么问题,除了你正在创建两个新连接......
尝试一下 - 以及链接文章中推荐的其他修复方法。祝你好运。
关于mysql - 打开从 VBA Excel 2007 到 MySQL 的连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14575422/