how to pass query variable to report? (1 Viewer)

Scatter

Registered User.
Local time
Today, 12:25
Joined
Dec 19, 2012
Messages
11
In Access2010, I have a crosstab report (monthly sum of hours by program) that runs off a button (OnClick event macro) on a main menu form. The data source is a query with the following SQL:

TRANSFORM Sum(Hours.Hours) AS SumOfHours
SELECT Hours.Program, Sum(Hours.Hours) AS [Total Hours]
FROM Hours
WHERE (((Year([VolDate]))=[What year?]))
GROUP BY Hours.Program
PIVOT Format([VolDate],"mmm") In ("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");

All runs well, but I want the year entered by the user to print in the report.
How can I pass the WHERE condition value entered by the user (a year) to be printed in the report?
 

llkhoutx

Registered User.
Local time
Today, 14:25
Joined
Feb 26, 2001
Messages
4,018
Using VBA, build the SQL string and store it as a query's SQL.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 15:25
Joined
Feb 19, 2002
Messages
43,257
Just use the same argument twice. Once in the select clause and again in the where clause. You won't be able to do this with the wizard but once the crosstab is created, you can open it and modify it directly.

TRANSFORM Sum(Hours.Hours) AS SumOfHours
SELECT Hours.Program, Sum(Hours.Hours) AS [Total Hours], [What year?] as WhatYear
FROM Hours
WHERE (((Year([VolDate])) = [What year?]))
GROUP BY Hours.Program
PIVOT Format([VolDate],"mmm") In ("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug"," Sep","Oct","Nov","Dec");
 

Scatter

Registered User.
Local time
Today, 12:25
Joined
Dec 19, 2012
Messages
11
Just use the same argument twice. Once in the select clause and again in the where clause. You won't be able to do this with the wizard but once the crosstab is created, you can open it and modify it directly.

TRANSFORM Sum(Hours.Hours) AS SumOfHours
SELECT Hours.Program, Sum(Hours.Hours) AS [Total Hours], [What year?] as WhatYear
FROM Hours
WHERE (((Year([VolDate])) = [What year?]))
GROUP BY Hours.Program
PIVOT Format([VolDate],"mmm") In ("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug"," Sep","Oct","Nov","Dec");

This works, as it leaves me with WhatYear to work with in the report.
But, it makes the user enter the year twice.
Can there be a variation with only one prompt?
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 15:25
Joined
Feb 19, 2002
Messages
43,257
You are getting prompted twice because the parameter names are not identical.
I don't see a parameter definition but most likely the problem is the names are not identical.
 

Users who are viewing this thread

Top Bottom