Results 1 to 10 of 10
  1. #1
    Jeans is offline Novice
    Windows 10 Access 2016
    Join Date
    Feb 2021
    Posts
    5

    report print preview blocking VBA code

    Hi,

    This is my first message on this forum, I will try to be as clear as possible.

    I am trying to preview a report before printing it.
    I want the user to:

    1) Click on the button "Print report" on a form.
    2) The report pops up in a print preview window

    3) Just when the preview opens, I want a pop up to confirm printing. ("Yes" to print the preview and close it or "No" to just close the preview)

    I have no issue with 1) and 2), but the printing confirm window appears before the preview is displayed, and blocks the operation. So I cannot actually preview the file before the printing confirmation.



    This is the code from my "Print report" button on my form (I also tried with just report view and not preview, without success):

    Code:
    Private Sub cmdPrintReport_Click()
        DoCmd.OpenReport "rptCardBack", acViewPreview, , , acDialog
    End Sub
    This is the code on my form, on open (I also tried on activate, page and load without success):

    Code:
    Private Sub Report_Open(Cancel As Integer)
        If MsgBox("Do you want to print?", vbYesNo, "Print?") = vbYes Then
            DoCmd.PrintOut
            DoCmd.Close acReport, "rptCardBack", acSaveNo
        Else:
            DoCmd.Close acReport, "rptCardBack", acSaveNo
            End If
    End Sub
    When I click "No" at the MsgBox, I get an error 2585 "This action can't be carried out whil processing a form or report event" linked to the piece of code that closes the report.
    I do not have this error when I try to open the report on viewreport instead of viewpreview, but it still doesn't display the report!

    I hope I was clear enough, but don't hesitate to ask for more information.
    Thank you for helping me!

    Nicolas

  2. #2
    Welshgasman is offline Expert
    Windows 10 Access 2007
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    622
    I would have thought all the code would be in the button click event.?

    You open the form in preview mode
    You display the message box
    Depending on the response, you either print the report or close the report
    Please, please use # when posting code snippets.
    Cross Posting Etiquette: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?search_query=debug+access+vba



  3. #3
    isladogs's Avatar
    isladogs is offline Very idle programmer
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    4,687
    Normally the user will click the Print button on the Print Preview ribbon. To my mind, your confirmation message should be scrapped.
    Colin (Mendip Data Systems): Website, email
    Try again. Fail again. Fail better.
    A
    sking for help isn't giving up. Its refusing to give up.

  4. #4
    Jeans is offline Novice
    Windows 10 Access 2016
    Join Date
    Feb 2021
    Posts
    5
    I plan on hiding the ribbon for users. Also, this project is to replace a program where users are used this pop up confirmation window (and they are reluctant to change ^^). Thank you !

  5. #5
    Jeans is offline Novice
    Windows 10 Access 2016
    Join Date
    Feb 2021
    Posts
    5
    Quote Originally Posted by Welshgasman View Post
    I would have thought all the code would be in the button click event.?

    You open the form in preview mode
    You display the message box
    Depending on the response, you either print the report or close the report
    Thank you, Welshgasman, I will try that. I does indeed make more sense.

  6. #6
    davegri's Avatar
    davegri is online now Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    2,657
    As Welshgasman says:

    Code:
    Private Sub cmdPrintReport_Click()
        DoCmd.OpenReport "rptCardBack", acViewPreview
        DoCmd.RunCommand acCmdFitToWindow
        If MsgBox("Print Report?", vbYesNo) = vbYes Then
            DoCmd.PrintOut
            DoCmd.Close acReport, "rptCardBack"
        Else
            DoCmd.Close acReport, "rptCardBack"
        End If
    End Sub
    Last edited by davegri; 02-27-2021 at 08:52 AM. Reason: syntax

  7. #7
    Jeans is offline Novice
    Windows 10 Access 2016
    Join Date
    Feb 2021
    Posts
    5
    Quote Originally Posted by Welshgasman View Post
    I would have thought all the code would be in the button click event.?

    You open the form in preview mode
    You display the message box
    Depending on the response, you either print the report or close the report
    I tried, but then I have to wait to close the preview window before the MsgBox appears. I would like the MsgBox to appear at the same time as the preview, then my answer to the MsgBox will close the preview.
    Do you know if this is possible?

    Here is now my code:
    Code:
    Private Sub cmdPrintReport_Click()
        DoCmd.OpenReport "rptCardBack", acViewPreview, , , acDialog
        If MsgBox("Do you want to print?", vbYesNo, "Print?") = vbYes Then
            
            DoCmd.PrintOut
            DoCmd.Close acReport, "rptCardBack", acSaveNo
        Else:
            
            DoCmd.Close acReport, "rptCardBack", acSaveNo
            End If
    End Sub

  8. #8
    davegri's Avatar
    davegri is online now Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    2,657
    I think the acDialog is the problem. Remove that.

  9. #9
    Jeans is offline Novice
    Windows 10 Access 2016
    Join Date
    Feb 2021
    Posts
    5
    Quote Originally Posted by davegri View Post
    I think the acDialog is the problem. Remove that.
    THANK YOU! That was the issue. It works perfectly now.

  10. #10
    isladogs's Avatar
    isladogs is offline Very idle programmer
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    4,687
    Quote Originally Posted by Jeans View Post
    I plan on hiding the ribbon for users. Also, this project is to replace a program where users are used this pop up confirmation window (and they are reluctant to change ^^). Thank you !
    I normally hide the ribbon as well but restore it whilst a report is open in print preview. When the report is closed, the ribbon is hidden again using code.
    Colin (Mendip Data Systems): Website, email
    Try again. Fail again. Fail better.
    A
    sking for help isn't giving up. Its refusing to give up.

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

Similar Threads

  1. Replies: 1
    Last Post: 12-13-2018, 01:06 PM
  2. Replies: 3
    Last Post: 04-28-2017, 09:51 AM
  3. Replies: 5
    Last Post: 08-06-2015, 03:26 PM
  4. Replies: 1
    Last Post: 02-21-2015, 11:35 PM
  5. Replies: 6
    Last Post: 03-01-2014, 07:07 AM

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 - Senior Forums