Page 1 of 2 12 LastLast
Results 1 to 15 of 19
  1. #1
    3 Star Lounger
    Join Date
    Mar 2001
    Location
    Frederick, Maryland, USA
    Posts
    296
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Printing Reports (A2K SR2)

    After I close a particular report (purchase order), I want 3 additional, text-only reports (terms and conditions) to print automatically, without user input. I've tried VBA code in the On Close event of the purchase order, but I can't seem to get it right. In code, I thought it would be as simple as opening up the form, printing it, closing it, then moving on to the next 2 forms. It obviously doesn't work that way! Any suggestions would be appreciated. Thank you.

  2. #2
    Super Moderator
    Join Date
    Aug 2001
    Location
    Evergreen, CO, USA
    Posts
    6,623
    Thanks
    3
    Thanked 60 Times in 60 Posts

    Re: Printing Reports (A2K SR2)

    Probably need more details, but what happens when you close the report - nothing? The scenario you describe should actually work, though print only reports have quite a few limitations. Try setting a debug stop on your first line of VBA and see if the event is actually firing off into code.
    Wendell

  3. #3
    3 Star Lounger
    Join Date
    Mar 2001
    Location
    Frederick, Maryland, USA
    Posts
    296
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Printing Reports (A2K SR2)

    The following code does the job for me:

    Private Sub Report_Close()
    DoCmd.OpenReport "rptPOTANDCPG1"
    DoCmd.OpenReport "rptPOTANDCPG2"
    DoCmd.OpenReport "rptPOTANDCPG3"
    End Sub

    Now, I'd like these 3 additional reports to print ONLY if the user PRINTS the main report. I tried adding this code to the On Print events in the main report Detail and Footer sections...the 3 additional reports will print before the main report is displayed in Print Preview, but the main report will not print (it will not queue to the printer). If I add this code to the On Close event of the main report, the main report and the 3 additional reports will print, but this results in the 3 additional reports ALWAYS printing, even if the user does not print the main report. How can I get the 3 additional reports to print ONLY if the use prints the main report? Thanks for the help.

  4. #4
    Silver Lounger GARYPSWANSON's Avatar
    Join Date
    Aug 2001
    Location
    Frederick, Maryland, USA
    Posts
    1,788
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Re: Printing Reports (A2K SR2)

    Does the user print the main report from a command button. If so, have the command button launch a macro to print out the other reports or build the printing of the other reports via code. (Good morning Roy)

    <img src=/S/hello.gif border=0 alt=hello width=25 height=29>
    Regards,

    Gary
    (It's been a while!)

  5. #5
    3 Star Lounger
    Join Date
    Mar 2001
    Location
    Frederick, Maryland, USA
    Posts
    296
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Printing Reports (A2K SR2)

    Morning, Gary. The user prints the main report (Purchase Order) by going to File/Print or by pressing the Print command button on the Access command bar. I have not created a custom print button just for the PO report.

  6. #6
    WS Lounge VIP rory's Avatar
    Join Date
    Dec 2000
    Location
    Burwash, East Sussex, United Kingdom
    Posts
    6,280
    Thanks
    3
    Thanked 191 Times in 177 Posts

    Re: Printing Reports (A2K SR2)

    Hi,
    I think the following code should work for what you want:
    <pre>Option Compare Database
    Option Explicit
    Dim blnPrinted As Boolean

    Private Sub Report_Activate()
    blnPrinted = True
    End Sub

    Private Sub Report_Close()
    If blnPrinted Then
    DoCmd.OpenReport "rptPOTANDCPG1"
    DoCmd.OpenReport "rptPOTANDCPG2"
    DoCmd.OpenReport "rptPOTANDCPG3"
    end if

    End Sub

    Private Sub Report_Deactivate()
    blnPrinted = False
    End Sub

    Private Sub ReportHeader_Print(Cancel As Integer, PrintCount As Integer)
    blnPrinted = Not blnPrinted
    End Sub
    </pre>

    This should ignore print previewing and only run the reports if your initial report is actually printed. I have just thought of one flaw in it though - if the user previews the report, deactivates that window and then reactivates it and shuts it without printing, the other three reports will still be printed. I will try and figure a way around that.
    Hope that helps.
    Regards,
    Rory

    Microsoft MVP - Excel

  7. #7
    WS Lounge VIP rory's Avatar
    Join Date
    Dec 2000
    Location
    Burwash, East Sussex, United Kingdom
    Posts
    6,280
    Thanks
    3
    Thanked 191 Times in 177 Posts

    Re: Printing Reports (A2K SR2)

    OK, I think this will work:
    <pre>Option Compare Database
    Option Explicit
    Dim blnPrinted As Boolean
    Private Sub Report_Open()
    blnPrinted = True
    End Sub
    Private Sub Report_Close()
    If blnPrinted Then
    DoCmd.OpenReport "rptPOTANDCPG1"
    DoCmd.OpenReport "rptPOTANDCPG2"
    DoCmd.OpenReport "rptPOTANDCPG3"
    blnPrinted = False
    end if
    End Sub
    Private Sub ReportHeader_Print(Cancel As Integer, PrintCount As Integer)
    blnPrinted = Not blnPrinted
    End Sub
    </pre>

    Regards,
    Rory

    Microsoft MVP - Excel

  8. #8
    3 Star Lounger
    Join Date
    Mar 2001
    Location
    Frederick, Maryland, USA
    Posts
    296
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Printing Reports (A2K SR2)

    Rory,

    I added your code to my main report, placing the first 3 lines in the Declarations section. The report does not run, however, and I am presented with the following error message:

    The expression On Open you entered as the event property setting produced the following error:
    Procedure declaration does not match description of event or procedure having the same name.
    * The expression may not result in the name of a macro, the name of a user-defined function, or [Event Property].
    * There may have been an error evaluating the function, event, or macro.

    Any ideas?

  9. #9
    WS Lounge VIP rory's Avatar
    Join Date
    Dec 2000
    Location
    Burwash, East Sussex, United Kingdom
    Posts
    6,280
    Thanks
    3
    Thanked 191 Times in 177 Posts

    Re: Printing Reports (A2K SR2)

    Hi,
    Did you just copy the code into a module? If so, I suspect that's the problem. (My fault really, I was being lazy and just copied the whole lot into my post!) You need to actually set each event procedure up in report design view - i.e. select the Open event for the report, choose Code Builder and then paste my Open event code into the procedure Access creates for you etc. Don't forget to clear out the code procedures you've already pasted in first.
    Hope that helps.
    Regards,
    Rory

    Microsoft MVP - Excel

  10. #10
    3 Star Lounger
    Join Date
    Mar 2001
    Location
    Frederick, Maryland, USA
    Posts
    296
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Printing Reports (A2K SR2)

    Rory,

    Many thanks! Again, you solved one of my problems. Thank you.

    A quick question...I can follow the logic of the Report Open and Report Close events. What does the ReportHeader_Print code do?

  11. #11
    WS Lounge VIP rory's Avatar
    Join Date
    Dec 2000
    Location
    Burwash, East Sussex, United Kingdom
    Posts
    6,280
    Thanks
    3
    Thanked 191 Times in 177 Posts

    Re: Printing Reports (A2K SR2)

    The report_print code simply switches the blnPrinted variable between true and false. The reason for the code being the way it is, is that the print event fires when you preview the report and then fires again if you actually print the report. So the open event sets it to True and the print event triggered by previewing sets it back to false. If the report is actually printed, the print event fires a second time and changes blnPrinted back to true, which in turn triggers the printing of your other reports when the report closes. If the report has only been previewed then blnPrinted = False so the other reports don't print.
    In the words of Homer Simpson: "Confused, would we?" <img src=/S/grin.gif border=0 alt=grin width=15 height=15>
    Regards,
    Rory

    Microsoft MVP - Excel

  12. #12
    3 Star Lounger
    Join Date
    Mar 2001
    Location
    Frederick, Maryland, USA
    Posts
    296
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Printing Reports (A2K SR2)

    Thanks again, Rory. You know, you really should consider doing this for a living!

  13. #13
    WS Lounge VIP rory's Avatar
    Join Date
    Dec 2000
    Location
    Burwash, East Sussex, United Kingdom
    Posts
    6,280
    Thanks
    3
    Thanked 191 Times in 177 Posts

    Re: Printing Reports (A2K SR2)

    I've got a long way to go before I'd be ready for that! Anyway, as soon as it became work, it wouldn't be fun anymore.... <img src=/S/smile.gif border=0 alt=smile width=15 height=15>
    Regards,
    Rory

    Microsoft MVP - Excel

  14. #14
    3 Star Lounger
    Join Date
    Mar 2001
    Location
    Frederick, Maryland, USA
    Posts
    296
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Printing Reports (A2K SR2)

    Rory,

    Your code works perfectly for a one page report. However, when my purchase order gets lengthy, and spans multiple pages, I run into trouble. I know I didn't mention that multiple page reports were a possibility. In my case, if the user views the additional report pages in Print Preview, the 3 additional pages print upon exiting Print Preview without printing the main report. There are also some other wierd, logic-related printing problems, too. I haven't had time to digest the code, but I'm sure it has to do with even and odd pages, the firing of the print command, and the use of Boolean logic to correct that. Is there a way to accommodate the possibility of multiple page main reports and the need to have the 3 additional reports print ONLY if the user prints the main report, regardless of length? Thank you for your help.

  15. #15
    WS Lounge VIP rory's Avatar
    Join Date
    Dec 2000
    Location
    Burwash, East Sussex, United Kingdom
    Posts
    6,280
    Thanks
    3
    Thanked 191 Times in 177 Posts

    Re: Printing Reports (A2K SR2)

    Hi,
    Did you definitely put the ReportHeader_Print code into the Print section of the Report Header (and not the PageHeader for example)? It sounds like that code is firing multiple times which it shouldn't be as the Header should only be on the first page of the report.
    Regards,
    Rory

    Microsoft MVP - Excel

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •