Link to home
Start Free TrialLog in
Avatar of MarkZipperer
MarkZipperer

asked on

Run a formatted report with seperate queries by creating seperate command buttons

I want to have an Access form from which I can run a report (Report1) using Query1 which will filter for all of the records created in a given year (for instance) by hitting CommandButton1 and then have another Command Button (CommandButton2) run Report1 with Query2 which will list all of the records sorted in Ascending date order and so forth.  In effect, I want to eliminate the need to have several copies of the same report which need to be update every time the format of the report needs to change.  
How do I select the query with code of the command button rather than in the Record Source on properties page of the report while in design view?
Avatar of dmitryz6
dmitryz6

Use open argument

On first button
DoCmd.OpenReport "Report1", acViewPreview, , , , "Q1"

On first button
DoCmd.OpenReport "Report1", acViewPreview, , , , "Q2"


On report open event
if me.OpenArgs="Q1" then
   me.RecordSource="Select * from Query1"
else
  me.RecordSource="Select * from Query2"
end if
ASKER CERTIFIED SOLUTION
Avatar of dmitryz6
dmitryz6

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial