我正在尝试使用excel更新mysql数据库,它只在mysql数据库的一列上运行良好,但如果其他列。。excel vba返回此错误
“运行时错误'-2147217900(80040e14)'”
这是我的密码
Sub ud()
Dim cmdCommand As New ADODB.Command
Dim recSet As New ADODB.Recordset
Dim cn As ADODB.Connection
Dim i As Integer
Dim sheet1 As Worksheet
Dim code, datesold As String
tf = Range("O2")
sf = Range("H3")
ActiveWorkbook.ActiveSheet.Activate
Set sheet1 = ActiveWorkbook.ActiveSheet
Set Rng = Range("Table_Query_from_20172018_1")
Set cn = New ADODB.Connection
cn.ConnectionString = "Driver=MySQL ODBC 5.3 ANSI
Driver;SERVER=localhost;PWD=12345678;UID=root;DATABASE=bio;PORT=3306"
cn.Open
cmdCommand.ActiveConnection = cn
Loop sheet1 data rows
For i = 1 To Rng.Rows.Count
ID = Rng.Cells(i, 5).Value
shf = Rng.Cells(i, 6).Value
wazaef = Rng.Cells(i, 7).Value
emtihan = Rng.Cells(i, 11).Value
strSQLCommand = "UPDATE students INNER JOIN (e1a INNER JOIN (eshfwi INNER JOIN wanda ON eshfwi.ID = wanda.ID) ON e1a.ID = wanda.ID) ON students.ID = e1a.ID SET eshfwi." & tf & " = " & shf & ", wanda." & tf & " = " & wazaef & ", e1a." & tf & " = " & emtihan & " where eshfwi.ID = " & ID & " and wanda.ID=" & ID & " and e1a.ID=" & ID & ";"
cmdCommand.CommandText = strSQLCommand
cmdCommand.CommandType = adCmdText
Set recSet = cmdCommand.Execute
Next i
End Sub
update:sql语句正在处理同一表中的另一列。。但是,当我更新另一个时,这个错误正在显示。。现在我尝试删除列并创建一个新列。。代码与某些id一起工作,然后相同的错误出现在包含此消息的报告中
https://i.stack.imgur.com/o4cy0.jpg
2条答案
按热度按时间yyyllmsg1#
我改了密码。。它现在的工作方式是创建一个带有连接字符串的表,其中包含sql更新命令,然后使用“for next”按id循环该命令:
qrjkbowd2#
很可能sql语句有错误。
发表声明
Debug.print strSQLCommand
在你写信给commandText
,检查立即窗口中的内容,并尝试在sql控制台上执行它。由于我们不知道您的数据类型,很难说,但可能您缺少引号。假设tf
是文本字段,代码应为... SET eshfwi." & tf & " = '" & shf & "'" ...
(提示:最好使用参数,例如https://codereview.stackexchange.com/questions/144063/passing-multiple-parameters-to-an-sql-query)