Results 1 to 3 of 3
  1. #1
    eortiz is offline Novice
    Windows 10 Access 2016
    Join Date
    Mar 2019
    Posts
    1

    Export to Excel, multiple files by filter

    Hi

    I have a macro set it up to export a report and creates a pdf for each value filtered. However, I tried to do the same to export a Query to Excel, but it is not working.

    This is the macro that works (exporting Report to pdf)
    Option Compare Database
    Sub Print_by_AMP()



    Dim MyPath As String
    Dim rs As Recordset

    MyPath = "H:\test"


    Set rs = CurrentDb.QueryDefs("Q_AMP_ID").OpenRecordset

    rs.MoveFirst
    While Not rs.EOF
    DoCmd.OpenReport "Rpt_AMP_Summary_Report", acViewPreview, , "AMP_ID = " & rs("AMP_ID") ' & "'"
    DoCmd.OutputTo acOutputReport, "", acFormatPDF, MyPath & rs("AMP_ID") & "_AMP_Perform_Summ_2018Q2YTD.pdf", False
    DoCmd.Close acReport, "Rpt_AMP_Summary_Report"

    rs.MoveNext
    Wend

    End Sub



    This is the macro that does not work (exporting query yo Excel). The error is: Wrong number of arguments or invalid property assignment.

    Option Compare Database
    Sub Print_by_AMP2()

    Dim MyPath As String
    Dim rs As Recordset

    MyPath = "H:\test"


    Set rs = CurrentDb.QueryDefs("Q_AMP_ID").OpenRecordset

    rs.MoveFirst
    While Not rs.EOF
    DoCmd.OpenQuery "Q_AMP_RUNNING_BALANCE", acViewPreview, , "AMP_ID =" & rs("AMP_ID") ' & "'"
    DoCmd.OutputTo acOutputQuery, , acFormatXLSX, MyPath & rs("AMP_ID") & "_AMP_Perform_Summ_test.xlsx", False
    DoCmd.Close acQuery, "Q_AMP_RUNNING_BALANCE"

    rs.MoveNext
    Wend

    End Sub

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,643
    You can't assume that OpenQuery has the same arguments available as OpenReport; it doesn't. One option is adding a criteria to the query that points to a textbox on a form. Within your loop, populate that textbox with the value from the recordset before exporting.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    ijaz8883 is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Jan 2019
    Posts
    103

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 3
    Last Post: 04-07-2016, 04:36 AM
  2. Access Query - Export to multiple Excel files
    By WhosUsingMyName in forum Access
    Replies: 3
    Last Post: 05-22-2015, 03:53 PM
  3. Export one query to multiple Excel files
    By bliffer in forum Import/Export Data
    Replies: 3
    Last Post: 01-29-2014, 02:37 PM
  4. Replies: 11
    Last Post: 12-20-2012, 12:30 PM
  5. Export Access into multiple excel files based on field value
    By turntabl1st in forum Import/Export Data
    Replies: 7
    Last Post: 11-08-2012, 12:43 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums