【问题标题】:Trying to run a query with OLEDBConnection Run-time error '1004': Application-defined or object-defined error尝试使用 OLEDBConnection 运行时错误“1004”运行查询:应用程序定义或对象定义错误
【发布时间】:2023-04-09 22:55:01
【问题描述】:

我在 .Connection 上收到 1004 错误

    With ActiveWorkbook.Connections(ConnectionName).OLEDBConnection
    .BackgroundQuery = True
    .CommandText = MyCommandText
    .CommandType = xlCmdSql
    .Connection = ConnectionString

ConnectionString 如下所示:

"Provider=SQLOLEDB.1" & Chr(59) & "Integrated Security=SSPI" & Chr(59) & _
"Persist Security Info=True" & Chr(59) & "Initial Catalog=wss_back" & Chr(59) & _
"Data Source=XX-X-0009999.de.xxx.com,12345" & Chr(59) & _
"Use Procedure for Prepare=1" & Chr(59) & "Auto Translate=True" & Chr(59) & _
"Packet Size=4096" & Chr(59) & "Workstation ID=XX-X-1234567" & Chr(59) & _
"Use Encryption for Data=False" & Chr(59) & _
"Tag with column collation when possible=False"

任何想法为什么会失败?

【问题讨论】:

    标签: vba excel oledb ado oledbconnection


    【解决方案1】:

    问题出在一行:

        .CommandType = xlCmdSql
    

    CommandType 需要来自 ADODB.CommandTypeEnum 的值,但您提供的是来自 xlCmdType 的值。 xlCmdSql = 2,和ADODB.adCommandTypeEnum.adCmdTable一样,不是adCmdText,这就是你想要的。

    所以改成:

    With ActiveWorkbook.Connections(ConnectionName).OLEDBConnection
      .BackgroundQuery = True
      .CommandText = MyCommandText
      .CommandType = adCmdText
      .Connection = ConnectionString
    

    ...它应该可以工作。

    【讨论】:

      猜你喜欢
      • 2013-05-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-28
      • 1970-01-01
      • 2016-01-14
      相关资源
      最近更新 更多