【问题标题】:Update an excel sheet using VBA/ADO使用 VBA/ADO 更新 Excel 工作表
【发布时间】:2012-08-17 09:01:27
【问题描述】:

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

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

【问题讨论】:

  • constConfigFile 是什么样的?是否缺少写入该文件的文件权限?

标签: sql vba excel ado


【解决方案1】:

从您的连接字符串中删除 IMEX=1。这对我有用。

.ConnectionString = "Data Source=" & ActiveWorkbook.Path & constConfigFile & ";" & _
    "Extended Properties=""Excel 8.0;HDR=Yes;"";"

【讨论】:

  • 不错的收获! “IMEX=1; 告诉驱动程序始终将“混合”(数字、日期、字符串等)数据列作为文本读取。请注意,此选项可能会影响 excel 工作表写入访问权限。” connectionstrings.com/excel
  • 是的,同样的错误。 IMEX 的可能设置为:0,1,2。 (0 是导出模式;1 是导入模式;2 是链接模式(完全更新能力))
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多