Results 1 to 5 of 5
  1. #1
    Hemi426 is offline Novice
    Windows XP Access 2003
    Join Date
    Jul 2010
    Posts
    5

    One File Output per Access Recordset Record - Part1

    Greetings all, I am what would be considered an annual VB code developer, In other words, I just dont do it enough to be any good at it. So, I am hoping that one of the experts out there can help me accomodate the Winds of Change.
    For many moons I have produced a daily report for our local fire department that provided incident information for the previous days events. This was accomplished with an Access report (myReport) and a select query (myQuery). The select query pulls about 50 fields from a few SQL tables via ODBC. It normally has about 80 records for any given day. The select query is the record source for the report. This daily process is automated and generates one file (.RTF) that is about 80 pages long & it does not print to a printer. The powers that be have asked for a change with the following requirements.
    #1 - Each incident must now be printed to its own file (80 one page files a day instead of one 80 page)
    #2 - The output filename has to be formatted in a certain fashion (this will require one value from a field in the data)
    These changes are to accomodate an automatic import to a new document management system.


    I have looked around and come up with this code that I have leashed to a form load
    ------------------------
    Private Sub Form_Load()
    Dim db As DAO.Database
    Set db = CurrentDb
    Dim rec As DAO.Recordset
    Set rec = db.OpenRecordset("myQuery", dbOpenDynaset)
    Do While Not rec.EOF
    DoCmd.OutputTo acOutputReport, "myReport", acFormatRTF, "C:\Test\INC" & rec!Master_Incident_Number & ".rtf"
    rec.MoveNext
    Loop
    ------------------------
    It seems to loop properly and even produces one file per record, My problem is that each output file contains the entire recordset (probably the contents of myQuery). I need it to only contain the current record the recordset pointer is pointing to. I dont know how to tell the report (myReport) to use the current recordset record instead of the myQuery record source
    Thanks for any help :-)

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,619
    Need to apply filter to the report, open it, then output will use the open filtered report.

    1. open a recordset that has field for the criteria to filter the report
    Set rec = db.OpenRecordset("SELECT Master_Incident_Number FROM myQuery;", dbOpenDynaset)

    2. in loop code reference the criteria from recordset to open report and output
    DoCmd.OpenReport "reportname", , , "Master_Incident_Number=" & rec!Master_Incident_Number
    DoCmd.OutputTo acOutputReport, , acFormatRTF, "C:\Test\INC" & rec!Master_Incident_Number & ".rtf"
    DoCmd.Close acReport, "ReportName", acSaveNo
    rec.MoveNext
    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.

  3. #3
    Hemi426 is offline Novice
    Windows XP Access 2003
    Join Date
    Jul 2010
    Posts
    5
    Greetings, With the reference provided by June7, My new code looks like this

    Private Sub Form_Load()
    Dim db As DAO.Database
    Set db = CurrentDb
    Dim rec As DAO.Recordset
    Set rec = db.OpenRecordset("SELECT Master_Incident_Number FROM myQuery;", dbOpenDynaset)
    Do While Not rec.EOF
    DoCmd.OpenReport "myReport", , , "Master_Incident_Number=" & rec!Master_Incident_Number
    DoCmd.OutputTo acOutputReport, , acFormatRTF, "C:\Test\INC" & rec!Master_Incident_Number & ".rtf"
    DoCmd.Close acReport, "myReport", acSaveNo
    rec.MoveNext
    Loop

    Result is Access blows up and must restart Access - I tried changing

    DoCmd.OpenReport "myReport", , , "Master_Incident_Number=" & rec!Master_Incident_Number
    to
    DoCmd.OpenReport "myReport", , , "Master_Incident_Number" = rec!Master_Incident_Number

    And it doesnt blow up anymore but it hangs on the next step and fires a Runtime error 2487 - Object Type Argument for the action is blank or method is invalid. Debug takes me to the line below

    DoCmd.OutputTo acOutputReport, , acFormatRTF, "C:\Test\INC" & rec!Master_Incident_Number & ".rtf"

    Thanks

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,619
    The first DoCmd syntax is correct. The = must be within quotes.

    Sorry, need acViewPreview parameter, otherwise report goes straight to printer (default parameter).

    I made assumption that Master_Incident_Number is a number value. If it is text then use apostrophe delimiters.

    DoCmd.OpenReport "myReport", acViewPreview, , "Master_Incident_Number='" & rec!Master_Incident_Number & "'"
    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.

  5. #5
    Hemi426 is offline Novice
    Windows XP Access 2003
    Join Date
    Jul 2010
    Posts
    5
    Oh Yeah!! That works very nicely!! I have some other requirements but will need to run with this for a little while. Thank you so much June7!!!

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

Similar Threads

  1. Output form recordset clone to word
    By silverspr in forum Programming
    Replies: 1
    Last Post: 02-07-2013, 01:32 PM
  2. Replies: 0
    Last Post: 03-07-2012, 08:10 AM
  3. output file name
    By AdrianoG87 in forum Reports
    Replies: 4
    Last Post: 11-03-2011, 06:20 PM
  4. Output tables to an excel file on a network drive
    By GraemeG in forum Import/Export Data
    Replies: 1
    Last Post: 04-01-2011, 03:06 PM
  5. How would you output an XML file with an Ole Attachment?
    By techneophyte in forum Programming
    Replies: 7
    Last Post: 09-09-2010, 09:09 AM

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