Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Output MS Access Query to Excel with vba

Working with MS Access 2007, I have a query I'd like to run and export the results to a specific workbook in a saved Excel workbook. I have the following code written using DoCmd. First I open the query (this works) and then I try to output the results to excel.

DoCmd.OpenQuery "MyQueryName", acViewNormal, acEdit

DoCmd.OutputTo acOutputQuery, "Aging By Desk - Onboarding Team", acFormatXLS, _
    "filepath.SuperTest.xls", "SuperTest.xls", True

However, when this code is run, I get the following error message: "An Expression you entered is the wrong data type for one of the arguments". I've been playing around with each argument, but can't seem to locate the problem. Any ideas? Am I on the right path?

like image 556
RestitutorOrbis Avatar asked Aug 16 '13 18:08

RestitutorOrbis


People also ask

How do you get data from MS Access with Excel VBA code?

Step 1: Open the Excel Workbook and got to VBA code builder (Alt + F11) and Open a New Module. Step 2: Copy and Paste the below code in it. Step 3: Click the Run button or F5 (Also you can insert a button/shape in excel sheet then right click and assign this macro to run everytime.)


1 Answers

You've got too many arguments. From Microsoft's website:

expression.OutputTo(ObjectType, ObjectName, OutputFormat, OutputFile, AutoStart, TemplateFile, Encoding)

Take out one of those Excel filenames you have and it should work.

like image 140
Johnny Bones Avatar answered Nov 15 '22 06:11

Johnny Bones