gpt4 book ai didi

vba - 尝试使用 OLEDBConnection 运行时错误 '1004' : Application-defined or object-defined error 运行查询

转载 作者:行者123 更新时间:2023-12-04 19:46:05 24 4
gpt4 key购买 nike

我在 .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"

知道为什么会失败吗?

最佳答案

问题在于:

    .CommandType = xlCmdSql

CommandType 需要来自 ADODB.CommandTypeEnum 的值,但您提供的值来自 xlCmdTypexlCmdSql = 2,这与 ADODB.adCommandTypeEnum.adCmdTable 相同,而不是 adCmdText,这是您在这里想要的。

所以改成:

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

...它应该可以工作。

关于vba - 尝试使用 OLEDBConnection 运行时错误 '1004' : Application-defined or object-defined error 运行查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21754015/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com