Updating Excel data through SQL UPDATE

I would like to update the data of an Excel worksheet using an SQL query. I know that you can "connect" to a sheet through ADODB.Connection and get (SELECT) data from it in ADODB.Recordset. However, using the same process for an UPDATE query results in the error "Operation must use updatable query." Is there any other way to achieve this?

Example code that causes an error:

Sub SQLUpdateExample()
    Dim con As ADODB.Connection
    Dim rs As ADODB.Recordset
    Set con = New ADODB.Connection
    con.Open "Driver={Microsoft Excel Driver (*.xls)};" & _
           "DriverId=790;" & _
           "Dbq=" & ThisWorkbook.FullName & ";" & _
           "DefaultDir=" & ThisWorkbook.FullName
    Set rs = New ADODB.Recordset
    Set rs = con.Execute("UPDATE [Sheet1$] SET a = 10 WHERE b > 2")

    Set rs = Nothing
    Set con = Nothing
End Sub

The code expects to be in a saved .xls sheet, where Sheet1 includes a table with column headings (at least) a and b.

+3
source share
1 answer

I did not think that it was all to make it work ...


Excel ReadOnly .
ReadOnly=False .

Microsoft Support

+5

All Articles