我尝试在ADO Excel中使用此UPDATE语句有什么问题?

时间:2014-04-22 18:22:27

标签: sql excel vba adodb

我正在尝试使用ADO更新工作表的内容。

我构建的SQL语句是:

update [Regional Personnel$] set name='Ada Lovelace',
phone='(303) 555-1337',
[lan id]='ADL3',position='Engineer' where id=3

Regional Personnel工作表具有以下列结构:

+----+---------------------+------------------------+--------+----------------+
| ID |        Name         |        Position        | Lan Id |     Phone      |
+----+---------------------+------------------------+--------+----------------+
|  2 | Kimberly St.-Simone | Senior Engineer        | KMS1   | (808) 555-0210 |
|  3 | Daphne Christianson | Database Administrator | DAC5   | (909) 555-3524 |
+----+---------------------+------------------------+--------+----------------+

但是当我有一个ADO连接并尝试使用:

执行此更新语句时
Sub update(sql as string)

dim objconnection As New ADODB.Connection

objconnection.CommandTimeout = 99999999

objconnection.Open "Provider=Microsoft.ACE.OLEDB.12.0;" & _
    "Data Source=" & ThisWorkbook.FullName & ";" & _
        "Extended Properties=""Excel 12.0 Xml;HDR=YES;IMEX=1"";"


objconnection.Execute sql

End Sub

objconnection.Execute sql行会出现此错误:

Run-time error '-2147217900 (80040e14)':
Syntax error in UPDATE statement.

SQL语句对我来说没问题,我错过了什么吗?

2 个答案:

答案 0 :(得分:3)

  • position周围加上方括号(似乎是一个保留字)。
  • 从连接字符串中删除IMEX=1,否则会出现错误"操作 必须使用可更新的查询"

修订代码:

Sub Tester()

   update "update [Regional Personnel$] set name='Ada Lovelace'," & _
       " [Phone]='(303) 555-1337', [lan id]='ADL3', [position]='Engineer' where id=3"


End Sub


Sub update(sql As String)

    Dim objconnection As New ADODB.Connection

    objconnection.CommandTimeout = 99999999

    objconnection.Open "Provider=Microsoft.ACE.OLEDB.12.0;" & _
        "Data Source=" & ThisWorkbook.FullName & ";" & _
        "Extended Properties=""Excel 12.0 Xml;HDR=YES;"";"

    objconnection.Execute sql

End Sub

答案 1 :(得分:0)

您应该将表名和"位置"括起来。同样。

update [Regional Personnel$] 
set [Regional Personnel$].name='Ada Lovelace',
[Regional Personnel$].phone='(303) 555-1337',
[Regional Personnel$].[lan id]='ADL3',
[Regional Personnel$].[position]='Engineer' 
where [Regional Personnel$].id=3