Export from Access Querry to Excel

Experts:

I have a form with a button on it and I also have 3 Queries.  What I would like to have happen is—when the button is clicked, all 3 Queries get exported to 3 separate Excel Spreadsheets, as follows:

Querry1 goes to Spreadsheet1 located at C: /Matt/User/Desktop/Spreadsheet1
Querry2 goes to Spreadsheet2 C: /Matt/User/Desktop/Spreadsheet2
Querry3 goes to Spreadsheet3 C: /Matt/User/Desktop/Spreadsheet3

What I would like to happen is, when the button is clicked—each of these Queries get exported to their respective Spreadsheet.

Note:  I would like to overwrite any data within those spreadsheets every time an export is performed without receiving a Warning Dialog Box.

Also, once the export is complete, I would like the Access Database to display an 'Ok' Dialog Box that simply says "Report Has Been Exported"

Does anyone have a code I can place within my button?
mdstallaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Eric ShermanAccountant/DeveloperCommented:
Try this ...

    Dim strFileName as String
    strFileName = "C: /Matt/User/Desktop/Spreadsheet1.xls"
    DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "Querry1", strFileName, True
    strFileName = "C: /Matt/User/Desktop/Spreadsheet2"
    DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "Querry2", strFileName, True
    strFileName = "C: /Matt/User/Desktop/Spreadsheet3"
    DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "Querry3", strFileName, True
 
 MsgBox "Report Has Been Exported"


ET
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mdstallaAuthor Commented:
ET:

The code is producing an error.

It has a problem with:

DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "Querry1", strFileName, True

and

 DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "Querry2", strFileName, True

I don't know if it makes a difference but I'm using Access and Excel 2013

and

The spreadsheets are all .xlsx
0
Gustav BrockCIOCommented:
Try with:

    Const cstrFileName As String = "C: /Matt/User/Desktop/Spreadsheet{0}.xlsx"
    Const cstrQuery As String = "Querry{0}"

    Dim strFileName As String
    Dim strQuery As String
    Dim intFile As Integer

    For intFile = 1 To 3
         strFileName = Replace(cstrFileName, "{0}", CStr(intFile))
         strQuery = Replace(cstrQuery, "{0}", CStr(intFile))
         DoCmd.OutputTo acOutputQuery, strQuery, acFormatXlsx, strFileName, False, , , acExportQualityPrint
    Next
    MsgBox "Exported three reports.", vbInformation, "Export"

/gustav
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Eric ShermanAccountant/DeveloperCommented:
What error are you getting??

ET
0
tomfarrarCommented:
Maybe "acSpreadsheetTypeExcel12"?
0
mdstallaAuthor Commented:
Your code worked great.  Thanks E!
0
Eric ShermanAccountant/DeveloperCommented:
Thanks for the points.

ET
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.