Access to Excel Export

I am trying to export Records from a Access 2016 Database by a Date Range Query. The Code opens the database Window with the correct records from the query date range but when it transfers to the Excel file the entire database is exported.

Here is the VD Code used:

Private Sub Export_to_Excel_Click()
On Error GoTo ExportExcel_Err

    DoCmd.OpenQuery "allinfoExcel", acViewNormal, acEdit
    DoCmd.OutputTo acTable, "AMC products", "MicrosoftExcelBiff8(*.xls)", "I:\AMC Contact Mgmt Records.xls", False, "", 0
    Beep
    MsgBox "Check your I: Drive for the Excel File AMC Contact Mgmt Records!", vbOKOnly, ""


ExportExcel_Exit:
    Exit Sub

ExportExcel_Err:
    MsgBox Error$
    Resume ExportExcel_Exit
End Sub
goldhartIT SpecialistAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

NorieAnalyst Assistant Commented:
Your code is displaying the results from the query 'allinfoExcel' then outputting the table 'AMC products' to Excel.

If it's actually the query results you want in Excel try this.
DoCmd.OutputTo acQuery, "allinfoExcel", "MicrosoftExcelBiff8(*.xls)", "I:\AMC Contact Mgmt Records.xls", False, "", 0

Open in new window

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
goldhartIT SpecialistAuthor Commented:
Aww Man. I'm dusty. Thanks so much!!!!!!!!!
PatHartmanCommented:
Also, there is no reason to open the query at all.  I also don't ever use OutputTo to export to Excel.  I always use TransferSpreadsheet.  TransferSpreadsheet may give you additional options.
goldhartIT SpecialistAuthor Commented:
Would you mind an example replacing my code where necessary? Thank a bunch!
PatHartmanCommented:
Did Norie's suggestion not fix the problem?

Docmd.TransferText  will produce intellisense

Export the QUERY rather than the table since the query is where the selection criteria is.  Exporting the table will always export the entire table.
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
Databases

From novice to tech pro — start learning today.