Results 1 to 13 of 13
  1. #1
    visitnag is offline Novice
    Windows 11 Access 2021
    Join Date
    Dec 2022
    Posts
    17

    Access Report into multiple files depending on a grouped field

    Hi,

    I have a code which is running fine in creating a single file on a grouped field. The problem is I have more than 20000 records having
    DeptID, empId, JoinDt as 3 fields(i have kept sample fields).

    I have grouped on DeptId in which at least 20 records of employees for each Department.
    I am trying to print a separate file for each department as file name.

    When I execute the code every time its asking to enter the DeptId for each and every record...and creating only one DeptID file which appears first.

    As a newbie I dont know how to modify the code. Please help. Thank you



    Private Sub Command7_Click()

    Dim rst As Recordset


    Dim db As Database
    Dim strSQL As String


    Set db = CurrentDb()




    strSQL = "SELECT [MyTbl].DeptID, [MyTbl].empID, [MyTbl].joindt FROM [MyTbl]"
    Set rst = db.OpenRecordset(strSQL)
    rst.MoveFirst


    Do Until rst.EOF
    DoCmd.OpenReport "DeptRpt", acViewPreview, , "DeptID = " & rst!DeptID
    DoCmd.OutputTo acOutputReport, "DeptRpt", acFormatPDF, "C:\test" & rst!DeptID & ".pdf"
    DoCmd.Close acReport, "DeptRpt"
    rst.MoveNext
    Loop


    rst.Close
    Set rst = Nothing
    strSQL = ""



    End Sub

  2. #2
    Micron is offline Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,423
    Even after one coffee I'm not seeing why. Suggest you step through the code and watch your variables to ensure they are what you think. Also, if DeptID is text data type then you're missing delimiters, but I don't see that as being the reason. Also make sure you get records by testing the sql if you have not already.

    Please post code within code tags (# button on posting toolbar) to maintain indentation and readability.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  3. #3
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,250
    Does this work?
    Code:
    Private Sub Command7_Click()
    
    
    Dim rst As Recordset
    Dim db As Database
    Dim strSQL As String
    
    
    Set db = CurrentDb()
    
    
    strSQL = "SELECT DISTINCT [MyTbl].DeptID FROM [MyTbl];"
    Set rst = db.OpenRecordset(strSQL)
    rst.MoveFirst
    
    
    Do Until rst.EOF
    	DoCmd.OpenReport "DeptRpt", acViewPreview, , "DeptID = " & rst!DeptID
    	DoCmd.OutputTo acOutputReport, "DeptRpt", acFormatPDF, "C:\test" & rst!DeptID & ".pdf"
    	DoCmd.Close acReport, "DeptRpt"
    rst.MoveNext
    Loop
    
    
    rst.Close
    Set rst = Nothing
    Set db=Nothing
    
    
    
    
    End Sub
    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  4. #4
    visitnag is offline Novice
    Windows 11 Access 2021
    Join Date
    Dec 2022
    Posts
    17
    Yesss, Its working. Thank you..
    I am trying to accept the DeptID automatically by adding the following lines, but
    its writing the files on DeprtID but the records of the employees are same in
    all the files.

    Code:
    Do Until rst.EOF 
        MyField = rst("DeptID")
        MyPath = "C:\test\"
        OutPutFileName = MyField & ".pdf" 
            DoCmd.OpenReport "tmpRpt", acViewPreview, , MyField
            DoCmd.OutputTo acOutputReport, "tmpRpt", acFormatPDF, MyPath & OutPutFileName
            DoCmd.Close acReport, "tmpRpt"
        rst.MoveNext
    Loop
    rst.Close
    Set rst = Nothing
    strSql = ""

  5. #5
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,556
    I would not have the MoveFirst in there. If you get an empty recordset, you will get an error.

    I have never heard of opening a recordset/file and not being on the first record (if one exists?)
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  6. #6
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2013 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,250
    In the latest code you posted you lost the Where clause of the OpenReport method, so the report contains all the records:
    Code:
    DoCmd.OpenReport "DeptRpt", acViewPreview, , "DeptID = " & MyField
    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  7. #7
    visitnag is offline Novice
    Windows 11 Access 2021
    Join Date
    Dec 2022
    Posts
    17
    Quote Originally Posted by Gicu View Post
    In the latest code you posted you lost the Where clause of the OpenReport method, so the report contains all the records:
    Code:
    DoCmd.OpenReport "DeptRpt", acViewPreview, , "DeptID = " & MyField
    Cheers,
    Thank you for the reply,

    I have gone through the syntax of DoCmd.OpenReprt and applied the where cluase too,
    then its asking the group field name again whenever group Id changes. Its not
    taking automatically.

  8. #8
    visitnag is offline Novice
    Windows 11 Access 2021
    Join Date
    Dec 2022
    Posts
    17
    Thank you,
    Yes, even if the MoveFirst is commented out the result is same.

  9. #9
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    Other than unnecessary MoveFirst, I don't see anything wrong with original posted code. So why is there a parameter input popup?

    If you want to provide db for analysis, follow instructions at bottom of my post.
    Last edited by June7; 03-12-2023 at 03:43 AM.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  10. #10
    visitnag is offline Novice
    Windows 11 Access 2021
    Join Date
    Dec 2022
    Posts
    17
    Yes parameter value window is popping up...but i could not find any error in fields naming in the report
    Here I am attaching a very minimum sample db. Please check and help. Thank you
    Attached Files Attached Files

  11. #11
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2013 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,250
    Sorry, we all assumed your DeptID being a number when in fact it is a string. Can you check this updated version?
    I strongly recommend you enable the Require Variable Declaration=Option Explicit in you Access client for VBA;
    https://www.fmsinc.com/microsoftacce...ons/index.html

    Cheers,
    Attached Files Attached Files
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  12. #12
    visitnag is offline Novice
    Windows 11 Access 2021
    Join Date
    Dec 2022
    Posts
    17
    I should say sorry for not telling the DeptID is a string. Thank you a lot. Its working perfectly...

  13. #13
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    Now I understand cause of popup. Code passes filter criteria to report Filter property. But without text delimiters, report can't process the criteria and query engine kicks out a popup prompt. Place a breakpoint in code on Output line and view report in design view. Filter property show DeptID = D23568 when it needs to be DeptID = 'D23568'.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

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

Similar Threads

  1. Access Report to multiple PDFs FILES
    By vianda in forum Reports
    Replies: 3
    Last Post: 08-11-2020, 08:18 PM
  2. Replies: 4
    Last Post: 02-03-2016, 09:23 PM
  3. Replies: 8
    Last Post: 06-24-2015, 08:22 AM
  4. Replies: 10
    Last Post: 09-17-2014, 08:23 AM
  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

Tags for this Thread

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