数据库
首页 > 数据库> > mysql – VBA出错:`[Microsoft] [ODBC管理器]未找到数据源名称且未指定默认驱动程序

mysql – VBA出错:`[Microsoft] [ODBC管理器]未找到数据源名称且未指定默认驱动程序

作者:互联网

我正在创建一个宏来将我的Excel工作表中的数据添加到我的MySQL数据库中

但是当我运行宏时,我收到错误:

[Microsoft] [ODBC管理器]未找到数据源名称且未指定默认驱动程序

码:

Sub UpdateMySQLDatabasePHP()

' For detailed description visit http://www.vbaexcel.eu/

Dim Cn As ADODB.Connection
Dim Server_Name As String
Dim Database_Name As String
Dim User_ID As String
Dim Password As String
Dim SQLStr As String
Dim rs As ADODB.Recordset
Set rs = New ADODB.Recordset

Server_Name = Range("e4").Value             ' IP number or servername
Database_Name = Range("e1").Value         ' Name of database
User_ID = Range("h1").Value                      'id user or username
Password = Range("e3").Value                    'Password
Tabellen = Range("e2").Value                     ' Name of table to write to

rad = 0
While Range("a6").Offset(rad, 0).Value <> tom
TextStrang = tom
kolumn = 0
While Range("A5").Offset(0, kolumn).Value <> tom
    If kolumn = 0 Then TextStrang = TextStrang & Cells(5, 1) & " = '" & Cells(6 + rad, 1)
    If kolumn <> 0 Then TextStrang = TextStrang & "', " & Cells(5, 1 + kolumn) & " = '" & Cells(6 + rad, 1 + kolumn)
    kolumn = kolumn + 1
Wend



TextStrang = TextStrang & "'"
field2 = "cid"
field1 = "bid"
table1 = "MMbanner"
SQLStr = "UPDATE " & Tabellen & " SET " & TextStrang & "WHERE " & Cells(5, 1) & " = '" & Cells(6 + rad, 1) & "'"
Set Cn = New ADODB.Connection
Cn.Open "Driver={MySQL ODBC 3.51 Driver};Server=" & Server_Name & ";Database=" & Database_Name & _
";Uid=" & User_ID & ";Pwd=" & Password & ";"

Cn.Execute SQLStr
rad = rad + 1
Wend
Set rs = Nothing
Cn.Close
Set Cn = Nothing

End Sub

解决方法:

看起来连接字符串有问题.你有安装mySQL odbc驱动程序吗?

我发现测试连接的最简单方法是创建一个文本文件“New Text Document.txt”并将文件扩展名重命名为udl,这样你最终得到“New Text Document.udl”然后打开文件.它应该向您显示Datalink proerties向导.然后,您可以通过向导来创建和测试连接.获取连接字符串.用记事本打开“.udl”文件或将扩展名更改回“.txt”并用记事本打开它.

标签:mysql,excel,excel-vba,vba
来源: https://codeday.me/bug/20190630/1339673.html