Automation of Query results Export

runningonempty

New Member
Joined
Aug 7, 2007
Messages
35
Hi All

I need to create a query macro which brings up a query form. After entering criteria in form, the query runs, gets you the results and exports it to a new excel file, save as and awaits your input of the new excel file name, after which it defaults back to Access and awaits fresh input into the query form.

Please help!

Thanks
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Hi runningonempty, It sounds like you would need to create a query, form and macro. The query would have criteria based off the form fields. The macro would open the form (Action = OpenForm)... from the form you can add this code to a button to export the query.

Code:
Private Sub cmdExportQry_Click()
'Make sure you have reference to "Microsoft Excel xx Object Library" in Tools > References
On Error Resume Next

    Const strQryName = "Query1"    'change to the name of your query
    Dim strFileName As String
    Dim strInitName As String
    Dim strFileFilter As String

    'default the file to current directory with name of query
    strInitName = CurrentProject.Path & "\" & strQryName & ".xls"
    strFileFilter = "Excel Files (*.xls), *.xls"
    strFileName = Excel.Application.GetSaveAsFilename(strInitName, strFileFilter)
    
    'if you want the user to modify the excel file change false to true
    DoCmd.OutputTo acOutputQuery, strQryName, acFormatXLS, strFileName, False
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,925
Messages
6,122,298
Members
449,077
Latest member
Rkmenon

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