473,499 Members | 1,893 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Passing parameters to report record source subquery

Seth Schrock
2,965 Recognized Expert Specialist
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?
Dec 4 '15 #1
5 2557
zmbd
5,501 Recognized Expert Moderator Expert
Just a starting point, if you are using ACC2010 or newer there is the tempvars collection that you could use to pass the parameter.

As for a more on-point answer, I'll have to think thru what's happening when you send the data thru.

There is the on_load event in the report....

-z
Dec 4 '15 #2
jforbes
1,107 Recognized Expert Top Contributor
An option would be to use the existing Query Definition and do the parameter replacements yourself. Probably not the best solution, but it does have the advantage of maintaining the Query through the QBE Editor.

Something like this to do the substitution and set the report source:
Expand|Select|Wrap|Line Numbers
  1.     Dim oQueryDef As DAO.QueryDef
  2.     Dim sSQL As String
  3.  
  4.     Set oQueryDef = CurrentDb.QueryDefs("ParameterQuery")
  5.  
  6.     sSQL = oQueryDef.SQL
  7.     sSQL = Replace(sSQL, "[Parameter1]", "ValueToUse")
  8.  
  9.     Me.RecordSource = sSQL
  10.  
  11.     oQueryDef.Close
  12.  
  13.     Set oQueryDef = Nothing

If you were to use a Template Query Definition for the basis of the Query and then use a second query that is used as the Report RecordSource. You could then Edit the Template QueryDef through the QBE Editor and the use some code like the following to update the QueryDef that the Report would use:
Expand|Select|Wrap|Line Numbers
  1.     Dim oQueryDefTemplate As DAO.QueryDef
  2.     Dim oQueryDef As DAO.QueryDef
  3.     Dim sSQL As String
  4.  
  5.     Set oQueryDefTemplate = CurrentDb.QueryDefs("ReportQueryTemplate")
  6.     Set oQueryDef = CurrentDb.QueryDefs("ReportQuery")
  7.  
  8.     sSQL = oQueryDefTemplate.SQL
  9.     sSQL = Replace(sSQL, "[Parameter1]", "ValueToUse")
  10.     sSQL = Replace(sSQL, "[Parameter2]", "ValueToUse")
  11.     sSQL = Replace(sSQL, "[Parameter3]", "ValueToUse")
  12.     oQueryDef.SQL = sSQL
  13.  
  14.     oQueryDefTemplate.Close
  15.     oQueryDef.Close
  16.  
  17.     Set oQueryDefTemplate = Nothing
  18.     Set oQueryDef = Nothing
Dec 4 '15 #3
mbizup
80 New Member
The Tempvars collection, mentioned by zmbd would be the easiest/most straight forward thing for this, I think. They are similar to global variables in a sense, but don't lose values when errors are encountered like globals do. They can be used in property sheets, forms/reports, queries, code... See the following for an excellent explanation:
https://accessexperts.com/blog/2010/...2007-and-2010/

Set them in code prior to opening the report (YourTempVarName can be any name you choose, but avoid spaces, special characters, etc):

TempVars.add "YourTempVarName", YourTempVarValue

And then use them in your query, subquery, report filter property or anywhere else:

WHERE YourField = TempVars!YourTempVarName

Alternate syntax:

WHERE YourField = TempVars("YourTempVarName")
Dec 4 '15 #4
Seth Schrock
2,965 Recognized Expert Specialist
I think in this case I will use JForbes' idea. In looking over each time my report is called, there is one instance where I'm pulling a group of IDs, so I'm having to use the In() criteria and not the equals, so I don't think that I can use the TempVar method as that doesn't let me change from = 5585 to In(5585, 5600, 5616). Thank-you to everyone for your suggestions.
Dec 4 '15 #5
zmbd
5,501 Recognized Expert Moderator Expert
but can you not use
IN(tempvars("one"),tempvars("two"),tempvars("three "))

Now I have not tried this myself.... I've really just started playing with the tempvars object; however, it has proven very useful.
Dec 4 '15 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

1
2704
by: Justin Koivisto | last post by:
I am trying to create a report that displays a name of an advertising source and count of the number of times it was hit between certain date ranges. The data is split between two different...
3
2705
by: eddiec | last post by:
hi everyone, I have a report in an adp that uses aggregate functions in its record source and I am trying to figure out how to filter the records displayed in the report: DoCmd.OpenReport...
2
5250
by: Bob | last post by:
I'm new to Access projects and SQL server and am not a veteran VB programmer. There's a cry for help! I'm attempting to print the current form on screen by using a command button which the user...
1
3609
by: Maria | last post by:
Hello! I am new to Crystal reports an I have problems passing parameters form outside to Crystal report an creating a report with data from more than one table This is the problem: I have to...
6
5378
by: John | last post by:
Hi Is it possible to change the record source query of a report at runtime via code so the report can work with different data source queries? Thanks Regards
7
15869
by: wlc04 | last post by:
I would like to be able to set the record source of a report programatically, but I am not having much luck. I have 2 queries which could be the source depending on other parameters in the program,...
1
2806
by: wlc04 | last post by:
I would like to be able to set the record source of a report programatically, but I am not having much luck. I have 2 queries which could be the source depending on other parameters in the program,...
17
21131
by: kkk1979 | last post by:
I have been using access as front end and SQL as Back end. I need help in generating an access report, by using a stored procedure with input parameters as record source. I tried the following...
1
2488
by: Peter Herath | last post by:
I have created a report using sample codes taken by the forum. one problem is that the report displays the field/column names in the table in columnar format(field names display in rows.) but i want...
30
4151
by: CD Tom | last post by:
I found Stephen Lebans modReportToPDF and have been trying to get it to work in my application. I've downloaded his database and when I run his test it works fine. I then added it to my application...
0
7131
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7007
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7220
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5470
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4919
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4600
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3099
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1427
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
665
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.