OutputTo Macro that Passes Criteria to Query First

langlang98

New Member
Joined
Dec 8, 2010
Messages
16
I haven't found this question asked before so I apologize if it has and I have overlooked it. I have created a macro in Access that I am wanting to use the OutputTo Action to output a query to an Excel spreadsheet. What I am wanting to do it run this for multiple sales reps and have an output file for each rep. My thought was to have an OutputTo Action for each rep and have the action pass the rep number to the query for Criteria but I am having issues doing this. What I have done as an alternative is to have a query for each rep instead. I dabble with VBA enough to get some things done and am not a programmer by any means but am sure there has got to be a simple line of code or an option I am over looking that allows this. Below is the Code that I have.

Function Pivot_Table_Macro()
On Error GoTo Pivot_Table_Macro_Err
DoCmd.SetWarnings False
DoCmd.OpenQuery "Make MTD Table", acViewNormal, acEdit
DoCmd.OpenQuery "Make YTD Table", acViewNormal, acEdit
DoCmd.OutputTo acOutputQuery, "99945101", "MicrosoftExcelBiff8(*.xls)", "L:\Commercial Team Reports\HME Pivot Tables\RepPivotTables\99945101.xls", False, "", 0
DoCmd.OutputTo acOutputQuery, "99945301", "MicrosoftExcelBiff8(*.xls)", "L:\Commercial Team Reports\HME Pivot Tables\RepPivotTables\99945301.xls", False, "", 0
DoCmd.OutputTo acOutputQuery, "99945302", "MicrosoftExcelBiff8(*.xls)", "L:\Commercial Team Reports\HME Pivot Tables\RepPivotTables\99945302.xls", False, "", 0
MsgBox "Macro completed.", vbInformation, ""
DoCmd.SetWarnings True


Pivot_Table_Macro_Exit:
Exit Function
Pivot_Table_Macro_Err:
MsgBox Error$
Resume Pivot_Table_Macro_Exit
End Function


The blue font are the individual OutputTo Actions for each query. My thought would be to change it to something below but have Criteria pushed to it so that I have only a single query but the Criteria changes.

DoCmd.OutputTo acOutputQuery, "Pivot Table Query", "MicrosoftExcelBiff8(*.xls)", "L:\Commercial Team Reports\HME Pivot Tables\RepPivotTables\99945101.xls", False, "", 0

and have a line similar to below that would push the criteria to the query.

Queries![Pivot Table Query]![D2 Sales Rep_Label]='99945101'

Any help on this would be greatly appreciated!

Jim
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
The idea is that we have 50 reps and I want the macro to automatically run all 50 without user input as the reps don't change that often. And every month we are going to run this macro with minimal user input. (this isn't for ad hoc reporting, it is for monthly sales reports for all of the reps) This way I only have to update the macro once in a while as we add a new rep.

Jim
 
Upvote 0

Forum statistics

Threads
1,224,618
Messages
6,179,917
Members
452,949
Latest member
beartooth91

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top