0

所以,我必须在实习期间编写一段代码,将一些 Excel 工作表与 MySQL DB 链接并保持更新,到目前为止我有这个,但它一直给我错误。你看到有什么错误吗?谢谢阅读。

我得到的错误是:运行时错误-2147217900 自动化错误。

PS:我对VBA很陌生

Dim server_name As String
Dim database_name As String
Dim user As String
Dim Password As String
Dim rs As adodb.Recordset
Dim naam As String
Dim oConn As adodb.Connection


Public Sub getSerieNummer()

Dim result As String
Dim b As Long
Dim strSQL As String

naam = Range("C1").Value

server_name = "servername"
database_name = "dbname"
user_id = "idname"
Password = "password"

Set oConn = New adodb.Connection
oConn.Open "SERVER=" & server_name _
& ";PORT=3306" _
& ";DATABASE=" & database_name _
& ";UID=" & user_id _
& ";PWD=" & Password & _
";DSN=name_in_odbc;"

strSQL = "SELECT [serial_number] FROM view_aix WHERE [name] ='" & naam & "';"
Set rs = oConn.Execute(strSQL)

b = 0
With rs
    Do Until .EOF
    b = b + 1
    result = !serial_number
    rs.MoveNext
    Loop
End With
    oConn.Close
    rs.Close
Set rs.ActiveConnection = Nothing
Set oConn = Nothing
Range("C2").Value = result

End Sub
4

1 回答 1

0

MySQL 不支持“[”,而是使用“`”。

也许是您的连接字符串的问题。

ODBC 连接方式

Driver={mySQL};Server=localhost;Option=16834;Database=myDataBase;

OLEDB 连接方式

Provider=MySQLProv;Data Source=mydb;User Id=myUsername;Password=myPassword;
于 2012-10-07T20:16:17.463 回答