运行时错误'-2147217900(80040e14)'

时间:2018-01-08 08:15:01

标签: mysql excel vba

我正在尝试使用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

更新:sql语句正在使用同一个表中的另一列..但当我更新另一个时,这个错误显示..现在我试图删除列并创建一个新的..代码使用一些ID的然后出现相同的错误报告包含此消息

https://i.stack.imgur.com/o4cy0.jpg

2 个答案:

答案 0 :(得分:0)

最有可能是SQL语句出错 在将Debug.print strSQLCommand写入commandText之前放置一个语句tf,检查立即窗口中的内容并尝试在sql控制台上执行它。由于我们不知道您的数据类型,很难说,但也许您缺少引号。假设... SET eshfwi." & tf & " = '" & shf & "'" ...是文本字段,则代码应为
EDT
(提示:使用参数要好得多,例如参见https://codereview.stackexchange.com/questions/144063/passing-multiple-parameters-to-an-sql-query

答案 1 :(得分:0)

我改变了代码..现在它通过创建一个带有连接字符串的表包含sql更新命令然后使用"用于下一个"按ID循环命令:


Sub ud()
Dim i As Integer

Dim j As Integer

Dim k As Integer

 Dim sheet1 As Worksheet

 tf = Range("O2")

 sf = Range("H3")

Range("P8").Select

Application.CutCopyMode = False

With ActiveSheet.ListObjects.Add(SourceType:=0, Source:= _
        "ODBC;DSN=20172018;", Destination:=Range("$P$8")).QueryTable

.CommandText = Array("SELECT e1.ID FROM e1 where id=1")

.RowNumbers = False

.FillAdjacentFormulas = False

.PreserveFormatting = True

.RefreshOnFileOpen = False

.BackgroundQuery = False

.RefreshStyle = xlInsertDeleteCells

.SavePassword = False

.SaveData = True

.AdjustColumnWidth = True

.RefreshPeriod = 0

.PreserveColumnInfo = True

.ListObject.DisplayName = "Query_from_20172018t"

.Refresh BackgroundQuery:=False

End With


 Act
 iveWorkbook.ActiveSheet.Activate

 Set sheet1 = ActiveWorkbook.ActiveSheet

 Set Rng = Range("Table_Query_from_20172018_1")





 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

 With ActiveWorkbook.Connections("Connection").ODBCConnection

 .CommandText = Array("UPDATE students INNER JOIN e1a ON students.ID = e1a.ID SET e1a." & tf & " = " & emtihan & " where students.class='" & sf & "' and e1a.ID=" & ID & ";")

 End With

 ActiveWorkbook.RefreshAll

 Next i



 For j = 1 To Rng.Rows.Count

 ID = Rng.Cells(j, 5).Value

 shf = Rng.Cells(j, 6).Value

 wazaef = Rng.Cells(j, 7).Value

 emtihan = Rng.Cells(j, 11).Value

 With ActiveWorkbook.Connections("Connection").ODBCConnection

 .CommandText = Array("UPDATE students INNER JOIN eshfwi ON students.ID = eshfwi.ID SET eshfwi." & tf & " = " & emtihan & " where students.class='" & sf & "' and eshfwi.ID=" & ID & ";")

 End With

 ActiveWorkbook.RefreshAll

 Next j



For k = 1 To Rng.Rows.Count

ID = Rng.Cells(k, 5).Value

shf = Rng.Cells(k, 6).Value

 wazaef = Rng.Cells(k, 7).Value

 emtihan = Rng.Cells(k, 11).Value

 With ActiveWorkbook.Connections("Connection").ODBCConnection

 .CommandText = Array("UPDATE students INNER JOIN wanda ON students.ID = wanda.ID SET wanda." & tf & " = " & emtihan & " where students.class='" & sf & "' and wanda.ID=" & ID & ";")

End With

 ActiveWorkbook.RefreshAll

 Next k


End Sub