vba ADODB - 为什么我的记录集是只读的?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14080591/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 19:00:06  来源:igfitidea点击:

ADODB - why is my recordset read-only?

excelvbaadodbrecordset

提问by user1756095

In Excel, I'm using ADODB connection to build a recordset, fetching data from the worksheet in its own workbook as following:

在 Excel 中,我使用 ADODB 连接来构建记录集,从其自己的工作簿中的工作表中获取数据,如下所示:

Public Sub test()
    Dim cnn As New ADODB.Connection
    Dim rst As New ADODB.Recordset
    Dim strSQL As String
    Dim k As Variant

    cnn.Open "Provider=Microsoft.ACE.OLEDB.12.0;" & _
       "Data Source=" & ThisWorkbook.Path & "\" & ThisWorkbook.Name & ";" & _
        "Extended Properties=""Excel 12.0;HDR=No;IMEX=1;Readonly=False"";"

    strSQL = "SELECT F1 FROM [Workbench$];"

    rst.Open strSQL, cnn, adOpenStatic, adLockOptimistic
    rst.MoveFirst

    While Not rst.EOF
        rst("F1") = "NewValue"
        rst.Update
        rst.MoveNext
    Wend

End Sub

However, an error occurs while I run the code:

但是,运行代码时出现错误:

Run-time error '-2147217911 (80040e09)':
Cannot update. Database or object is read-only.

运行时错误“-2147217911 (80040e09)”:
无法更新。数据库或对象是只读的。

I have checked about permission of my opened workbook and it is ok (Full Control access for everyone).

我已经检查了我打开的工作簿的权限,它没问题(每个人都可以完全控制访问)。

What did I do wrong here?

我在这里做错了什么?

回答by Ivo

I fixed my problem by Use IMEX=0

我通过使用 IMEX=0 解决了我的问题

回答by mrsnax

rst.Open strSQL, cnn, adOpenStatic, adLockOptimistic

replace to

替换为

rst.Open strSQL, cnn, 1, 3