MS Access query with dynamic from statements

走远了吗. 提交于 2019-12-24 02:24:10

问题


Ok this is vexing me. I have a query I created with an in statement in the from clause. What I am looking to do is have a global variable populate that from statement. Example

Select *
Form query1 in <Global Variable filename>

What is going on is I link to a file that has hundreds of linked table and queries in it. My database only has a few select queries and link table to different database. I did not want to re-build all the queries and linked table is my database. The issue is the file with all the links changes name once a month. I just want to read a text file with the current name of the database in it so I do not have to keep changing my queries every time the database name changes. Also this has to a query since I have other queries using the externally linked query.


回答1:


I have one suggestion, but its pretty kludgy.

Rewrite the query on the fly with VBA call

Private Sub update_qtest()
Dim db As Database
Dim qd As QueryDef
    Set db = CurrentDb
    Set qd = db.QueryDefs("qtest")
    qd.SQL = "SELECT * from query1 in " & g_file_name
End Sub

As I said, it's kludgy, but I don't think there's a way to pass the from clause as a parameter.




回答2:


Another way to do this would be to just use the same file name each month so you wouldn't have to change anything in your Access app at all. You could easily code copying the file to the standard name over top of the previous copy (you'd have to delete it before copying, of course), which would retain the history.



来源:https://stackoverflow.com/questions/3495377/ms-access-query-with-dynamic-from-statements

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!