Microsoft KB Archive/316475

= PRB: &quot;Operation Must Use an Updateable Query&quot; Error Message When You Access Excel Through ODBC =

Article ID: 316475

Article Last Modified on 2/12/2007

-

APPLIES TO


 * Microsoft ActiveX Data Objects 2.5
 * Microsoft ActiveX Data Objects 2.6
 * Microsoft ActiveX Data Objects 2.7
 * Microsoft Visual Basic 6.0 Enterprise Edition
 * Microsoft Visual Basic 6.0 Professional Edition
 * Microsoft Excel 2002 Standard Edition
 * Microsoft Excel 97 Standard Edition

-



This article was previously published under Q316475



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 is 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 is not set to ReadOnly. Make sure that the file that you are trying to open is not saved as ReadOnly. If you are connecting through a DSN, follow these steps:  Open Control Panel, and then click ODBC Data Source Administrator. Double-click your DSN. In the ODBC Microsoft Excel Setup dialog box, click Options.</li> Make sure that the ReadOnly check box is not selected.</li></ol> </li>  If you are using a DSN-less connection, make sure to include the &quot;ReadOnly=0&quot; option in the connection string. For example: cn.Open &quot;Driver={Microsoft Excel Driver (*.xls)};DBQ=C:\MyDoc.xls;ReadOnly=0;&quot; </li></ul>

<div class="status_section">

STATUS
This behavior is by design.

<div class="moreinformation_section">

Steps to Reproduce the Behavior
<ol> Create a new Standard EXE project in Visual Basic.</li> On the Project menu, click References, and then add a reference to Microsoft ActiveX Data Objects Library.</li> Add a Command button to Form1.</li>  Add the following code to Form1: Dim rs As ADODB.Recordset Dim cn As ADODB.Connection

Private Sub Form_Load Command1.Caption = &quot;Edit&quot; End Sub

Private Sub Command1_Click Dim DocPath As String

DocPath = App.Path & &quot;\Test.xls&quot;

Set cn = New Connection Set rs = New Recordset

cn.Open &quot;Driver={Microsoft Excel Driver (*.xls)};DBQ=&quot; & DocPath & &quot;;ReadOnly=1&quot; rs.LockType = adLockOptimistic rs.Open &quot;TB1&quot;, cn rs.AddNew rs.Fields(1).Value = &quot;New Value&quot; rs.Update

rs.Close cn.Close Set rs = Nothing Set cn = Nothing End Sub </li> Save the application.</li> Create a new Excel worksheet, and then save the worksheet as Test.xls.</li> On the Insert menu, point to Name, and then click Define.</li> Create a new table inside the Excel worksheet, and then name the table TB1 .</li> Save the worksheet in the same folder as the Visual Basic application.</li> Press F5 to run the application.</li></ol>

<div class="references_section">