Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Symptoms
When you edit an Excel worksheet through ADO and ODBC, you may receive the following error message if you use an ADO DataControl object:
[Microsoft][ODBC Excel Driver] Operation must use an updateable query.
If you use a Recordset object that's generated with ADO code, you may receive the following error message when you edit an Excel worksheet through ADO and ODBC:
Run-time error '-2147467259(80004005)': [Microsoft][ODBC Excel Driver] Operation must use an updateable query.
Cause
This problem occurs if you try to edit a worksheet that is saved or opened as ReadOnly.
Note
ReadOnly is the default setting for an ODBC connection to Excel, with or without a data source name (DSN). Therefore, the user must always change that setting to edit data.
Resolution
To resolve this problem, use the following methods:
Make sure that the LockType property of the Recordset object isn't set to ReadOnly.
Make sure that the file that you're trying to open isn't saved as ReadOnly.
If you're connecting through a DSN, follow these steps:
- Open Control Panel, and then select ODBC Data Source Administrator.
- Double-click your DSN.
- In the ODBC Microsoft Excel Setup dialog box, select Options.
- Make sure that the ReadOnly check box is cleared.
If you're using a DSN-less connection, make sure to include the "ReadOnly=0" option in the connection string. For example:
cn.Open "Driver={Microsoft Excel Driver (*.xls)};DBQ=C:\MyDoc.xls;ReadOnly=0;"
Status
This behavior is by design.
More Information
Steps to Reproduce the Behavior
Create a new Standard EXE project in Visual Basic.
On the Project menu, select References, and then add a reference to Microsoft ActiveX Data Objects Library.
Add a Command button to Form1.
Add the following code to Form1:
Dim rs As ADODB.Recordset Dim cn As ADODB.Connection Private Sub Form_Load() Command1.Caption = "Edit" End Sub Private Sub Command1_Click() Dim DocPath As String DocPath = App.Path & "\Test.xls" Set cn = New Connection Set rs = New Recordset cn.Open "Driver={Microsoft Excel Driver (*.xls)};DBQ=" & DocPath & ";ReadOnly=1" rs.LockType = adLockOptimistic rs.Open "TB1", cn rs.AddNew rs.Fields(1).Value = "New Value" rs.Update rs.Close cn.Close Set rs = Nothing Set cn = Nothing End SubSave the application.
Create a new Excel worksheet, and then save the worksheet as Test.xls.
On the Insert menu, point to Name, and then select Define.
Create a new table inside the Excel worksheet, and then name the table TB1.
Save the worksheet in the same folder as the Visual Basic application.
Press F5 to run the application.
References
For more information, see Office Space: Tips and Tricks for Scripting Microsoft Office Applications.