4

当我执行这个函数时,我得到一个运行时错误,“操作必须使用可更新的查询”。是什么原因造成的?

Function updateConfigFile(strQuery As String)

Dim cnn As ADODB.Connection         
Dim objMyCmd As ADODB.Command

Set cnn = New ADODB.Connection
Set objMyCmd = New ADODB.Command

constConfigFile = "MyWorkbookName"

With cnn

    .Provider = "Microsoft.Jet.OLEDB.4.0"
    .ConnectionString = "Data Source=" & ActiveWorkbook.Path & constConfigFile & ";" & _
        "Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1"";"
    .Open

End With

strQuery = "update [test$] Set [test]='Hello' WHERE [Test]='h'"

objMyCmd.CommandType = adCmdText
objMyCmd.CommandText = strQuery
objMyCmd.ActiveConnection = cnn

objMyCmd.Execute


Set objMyCmd = Nothing
Set cnn = Nothing


End Function
4

1 回答 1

9

从连接字符串中删除 IMEX=1。这对我行得通。

.ConnectionString = "Data Source=" & ActiveWorkbook.Path & constConfigFile & ";" & _
    "Extended Properties=""Excel 8.0;HDR=Yes;"";"
于 2012-08-17T09:53:01.943 回答