Macro or Code to Export Filtered Report for all Unique Instances of a Specific Field

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • theberner
    New Member
    • Jan 2013
    • 8

    #1

    Macro or Code to Export Filtered Report for all Unique Instances of a Specific Field

    I have a report, Report A, that pulls data from a Table, Table A. Table A, has a field, Field A, where I am to export Report A for each Unique Entery in Field A. Currently I have to do this manually, but Ideally I am looking to find a code that can do the following.

    For Uniqe each value of Field A in Table A
    Filter Table A
    Run Report
    Export Report as PDF with naming convention of "value of Field A + YYYYMMDD"
    Remove Filter from Table A
    and then Repeat for each Unique value in Field A.

    Another way may be to filter Report A outright and then go about it that way.

    I have found codes online, including on this site, but since I am not well versed in VBA code, I keep running into snags when I try to manipulate them to fit my needs.

    Any help you can provide will be greatly appreciated.
  • Seth Schrock
    Recognized Expert Specialist
    • Dec 2010
    • 2965

    #2
    Please post what you have tried and what snags you got, either wrong results or error messages. We would need the EXACT error message and the error number. Please be sure to post your code in code tags (the <CODE/> button).

    Reading the following link will help you know what exactly we need in order to help you: How to ask "good" questions

    Also, please specify what part isn't working the way you want it to. From your question, I'm not sure if the problem is in finding the unique values of Field A in Table A, Filtering the table, running the report, Exporting your report, removing the filter, or repeating the process.

    Comment

    • theberner
      New Member
      • Jan 2013
      • 8

      #3
      Perhaps I mispoke. I do not have any code because the code I find does not fit my situation and any manipulation I do is basedon cluelessnes. So sending you code to fix would probably confuse me more as that code was manipulated from something that may have worked into the garbage that I thought would work. So really, i have nothing.
      My issue is with everything because I have no idea how to go about the macro or code. At best, I can create a macro to run a code, but I am not skilled enough to write it.
      I started to ask on forums and other sites because I do not know where even to begin and have been trying to figure this out for months.

      Comment

      • ADezii
        Recognized Expert Expert
        • Apr 2006
        • 8834

        #4
        I am running out the door, but the actual Code will look something like this. Any questions, I will be more than happy to answer.
        Code:
        Dim MyDB As DAO.Database
        Dim rs As DAO.Recordset
        Dim strSQL As String
        
        strSQL = "SELECT DISTINCT [Field A] FROM [Table A] ORDER BY [Field A]"
        
        Set MyDB = CurrentDb
        Set rs = MyDB.OpenRecordset(strSQL, dbOpenSnapshot)
        
        With rs
          Do While Not .EOF
            DoCmd.OpenReport "Report A", acViewDesign, , "[Field A] = '" & ![Field A] & "'", acHidden
            Reports![Report A].RecordSource = "SELECT * FROM [Table A] WHERE [Field A] = '" & ![Field A] & "'"
            DoCmd.OutputTo acOutputReport, "Report A", acFormatRTF, CurrentProject.Path & "\" & "Report_" & _
                           ![Field A] & ".rtf", False
            DoCmd.Close acReport, "Report A", acSaveNo
              .MoveNext
          Loop
        End With
        
        rs.Close
        Set rs = Nothing

        Comment

        • theberner
          New Member
          • Jan 2013
          • 8

          #5
          ADezii, this works perfectly. Thank you so much for taking the time out to show me how this goes.

          Comment

          • NeoPa
            Recognized Expert Moderator MVP
            • Oct 2006
            • 32669

            #6
            As a moderator I have to support what Seth has posted about requesting solutions without first showing that you have attempted to get to grips with your own problem. This is not because we don't care, but simply that it leaves others to expect the same attitude to work - Please do my work for me is not an attitude we like to encourage.

            That said, your frank response leaves me feeling for you. I will leave this here for now at least, but just reinforce the point to all that read this that we are not a code-writing service. We are here to help others to understand better and to be able to develop their projects on their own.

            Comment

            • theberner
              New Member
              • Jan 2013
              • 8

              #7
              NeoPa, I understand your point and I apologize if I used the forum incorrectly. It was not intentional. I thought from other posts that my post would be ok even though I had no code. Now I know, and I will keep this in mind going forward. Thank you for your understanding of my situation.

              Comment

              Working...