I have a very large UNION query with both sides pulling from several tables and subqueries. Due to the nature of the query, it naturally runs very slowly: about six and a half minutes. To fix this, I'm having to put a WHERE clause in the subqueries to filter the foreign keys as well as in the main UNION query. This query is the basis for a report. Normally, I would just open the report, passing it a WhereCondition argument, but that only filters the main query, after all the joins have taken place. My problem how is getting this foreign key value to the subquery. My original thought was to change the SQL code using some Replace Statements that would add the appropriate WHERE conditions, but due to the large amount of text in the query, I'm not eager to copy all those lines and fix quotes, etc. into a VBA variable.
I have seen code that allows VBA to pass a parameter value to a query, but not if I'm calling the query as the record source for a report. Is there a way to do that? Or is there a better method?