excel VBA将查询中的参数传递给ADO连接“1004”:应用程序定义或对象定义的错误

hl0ma9xz  于 2023-01-31  发布在  其他
关注(0)|答案(1)|浏览(129)

在命令文本中,我似乎无法使用参数作为表名,因此我现在尝试将其硬编码到VBA查询中,但我遇到了“1004”:应用程序定义或对象定义的错误。附件是我正在使用但似乎不起作用的命令文本的屏幕截图。我添加的以下代码导致了该错误:The Connection Properties pop up box

With ActiveWorkbook.Connections("Query1").OLEDBConnection
        .BackgroundQuery = True
        .CommandType = adCmdText
        .CommandText = "SELECT * FROM [DBO].[Refresh_" & UserName & "] ORDER BY [Item No];"
End With

我将感谢任何人可以给予我关于查询“连接”属性框或vba代码的任何帮助。
先谢了保罗

fnx2tebb

fnx2tebb1#

根据您设置连接的方式,它将具有ODBCConnectionOLEDBConnection属性(但不能同时具有这两个属性)。
如果您稍微分解一下代码,就可以监视连接并查看填充了哪些属性:

Sub Tester()
    
    Dim conn As WorkbookConnection 'put a Watch on this and step through....
    
    Set conn = ThisWorkbook.Connections("TestQuery")
    
    With conn.ODBCConnection
            .BackgroundQuery = True
            .CommandType = xlCmdSql    '<<<<  not adCmdText
            .CommandText = "select * from ops$rs3.hts_scientist"
    End With

End Sub

监 windows 口:

相关问题