【问题标题】:Run-time error '-2147217900 (80040e14)'运行时错误'-2147217900 (80040e14)'
【发布时间】:2018-01-23 22:49:15
【问题描述】:

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

【问题讨论】:

    标签: mysql excel vba


    【解决方案1】:

    SQL 语句很可能有错误。
    在写到commandText之前先放一条语句Debug.print strSQLCommand,查看Immediate Window中的内容,然后尝试在sql控制台上执行。由于我们不知道您的数据类型,因此很难判断,但您可能缺少引号。假设tf是一个文本字段,代码应该是
    ... SET eshfwi." & tf & " = '" & shf & "'" ...
    (提示:最好使用参数,例如https://codereview.stackexchange.com/questions/144063/passing-multiple-parameters-to-an-sql-query

    【讨论】:

    • 感谢您的回复,请问您可以在问题的新更新中看到图片吗?
    • 所以这是一个 SQL 语法错误,但从这里不可能准确地说明问题所在。尝试直接对您的数据库执行它(而不是从 VBA)并更正它。
    【解决方案2】:

    我更改了代码..它现在通过创建一个包含 sql 更新命令的连接字符串的表来工作,然后使用“for next”按 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
    
    
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-05-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多