mysql - 打开从 VBA Excel 2007 到 MySQL 的连接

标签 mysql excel vba odbc database-connection

尝试使用 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/

相关文章:

vba - 如何从arrayList中提取图片对象

javascript - 使用 JavaScript 函数自动化网页

php - 从 SQL 数据库添加网站内容

mysql - 打开 MS SQL TCP 端口(安全)

php - 阻止用户注册带有空格的用户名

VBA - 在 a 范围内加 1 小时

excel - 如何添加技能编号

mysql - SQL 计数来自其他表的调用

sql-server - 从 Excel 导入到 SQL Server 2005 时出现截断错误

excel - 如何为图表制作从每周星期一开始的刻度标签?