Question

I'm trying to update the contents of a worksheet using ADO.

The SQL statement that I've constructed is:

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

The Regional Personnel worksheet has this column structure:

+----+---------------------+------------------------+--------+----------------+
| 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 |
+----+---------------------+------------------------+--------+----------------+

But when I have an ADO connection and try to execute this update statement using:

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

The objconnection.Execute sql line gives this error:

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

The SQL statement looks ok to me, am I missing something?

Was it helpful?

Solution

  • Put square brackets around position (seems to be a reserved word).
  • Remove the IMEX=1 from the connection string, or you get an error "operation must use an updateable query"

Revised code:

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

OTHER TIPS

You should bracket the table names and "position" as well.

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top