将 VBA 中的访问查询类型从传递 (ODBC) 查询动态更改为常规 Select 查询
Change Access Query Type Dynamically in VBA from a Pass Through (ODBC) Query to a regular Select Query
我对动态使用 Microsoft Access 查询很感兴趣,有时作为传递查询使用 ODBC 访问远程 SQL 服务器,有时作为本地 Select 查询访问table 在同一个 Access 数据库中。但是,QueryDef.Type 属性 是只读的,我不知道如何更改它。
所以在代码中它看起来像:
Dim qd As DAO.QueryDef
Set qd = CurrentDB.QueryDefs("qrySubForm1")
'this line turns the qd.Type property to dbQSQLPassThrough automatically I believe
qd.Connect = "ODBC;--connectionstring--"
qd.SQL = "Select * from SomeRemoteTable"
'populate the subform with the results of the Pass Through query
SubForm1.SourceObject = "Query.qrySubForm1"
'Intent: change to regular select query.
qd.Type = dbQSelect ' Error: read-only property
qd.SQL = "Select * from ALocalTable"
' now change the SubForm to show results of the local query
SubForm1.SourceObject = "Query.qrySubForm1"
QueryDef 类型 属性 是 this 枚举中的值之一。
如果我在两个本地查询之间切换,或者在两个传递查询之间切换,它工作正常。只有当我尝试在直通查询和本地查询之间切换时才会遇到问题。
更新:
This 对另一个 SO 问题的回答似乎表明我可以在查询定义中添加一个 属性,但我不确定这是否适用于 "Type."
正确的方法是删除并创建 QueryDef。
在更改为本地之前添加此 table:
DoCmd.DeleteObject acQuery, qd.name
Set qd= MyDB.CreateQueryDef(qd.name)
qd.SQL = "Select * from ALocalTable;"
qd.close
如果您想返回直通:
DoCmd.DeleteObject acQuery, qd.name
Set qd= MyDB.CreateQueryDef(qd.name)
qd.connect = GET_CONNECTION_STRING
qd.SQL = "Select * from SomeRemoteTable;"
qd.close
更新查询集 "qd.ReturnsRecords = false"
我对动态使用 Microsoft Access 查询很感兴趣,有时作为传递查询使用 ODBC 访问远程 SQL 服务器,有时作为本地 Select 查询访问table 在同一个 Access 数据库中。但是,QueryDef.Type 属性 是只读的,我不知道如何更改它。
所以在代码中它看起来像:
Dim qd As DAO.QueryDef
Set qd = CurrentDB.QueryDefs("qrySubForm1")
'this line turns the qd.Type property to dbQSQLPassThrough automatically I believe
qd.Connect = "ODBC;--connectionstring--"
qd.SQL = "Select * from SomeRemoteTable"
'populate the subform with the results of the Pass Through query
SubForm1.SourceObject = "Query.qrySubForm1"
'Intent: change to regular select query.
qd.Type = dbQSelect ' Error: read-only property
qd.SQL = "Select * from ALocalTable"
' now change the SubForm to show results of the local query
SubForm1.SourceObject = "Query.qrySubForm1"
QueryDef 类型 属性 是 this 枚举中的值之一。
如果我在两个本地查询之间切换,或者在两个传递查询之间切换,它工作正常。只有当我尝试在直通查询和本地查询之间切换时才会遇到问题。
更新: This 对另一个 SO 问题的回答似乎表明我可以在查询定义中添加一个 属性,但我不确定这是否适用于 "Type."
正确的方法是删除并创建 QueryDef。
在更改为本地之前添加此 table:
DoCmd.DeleteObject acQuery, qd.name
Set qd= MyDB.CreateQueryDef(qd.name)
qd.SQL = "Select * from ALocalTable;"
qd.close
如果您想返回直通:
DoCmd.DeleteObject acQuery, qd.name
Set qd= MyDB.CreateQueryDef(qd.name)
qd.connect = GET_CONNECTION_STRING
qd.SQL = "Select * from SomeRemoteTable;"
qd.close
更新查询集 "qd.ReturnsRecords = false"