Results 1 to 5 of 5
  1. #1
    Jester0001 is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Feb 2012
    Location
    Indiana
    Posts
    69

    VBA MsgBox Print .Echo Freeze

    The code below is for a form, and is triggered by the OnExit event of the subform. A MsgBox asks the user if they want to print a report. If they clicked yes, then the report opens in print preview. Next, the print dialog box appears. Here's where a problem occurs:

    If the print job is executed, then everything is okay. However, if the print job is canceled from the print dialog, then the application "freezes" because the screen repainting isn't turned back on. I could easily remove the screen repainting, but then the user has to see all of the background steps to print the report. What can be done to still have the screen repainting disabled at the beginning, and then enabled at the end, even if the user cancels the print job from the print dialog box?

    Thank you in advance



    Code:
    Private Sub subSubform_Exit(Cancel As Integer)
    'To show a Yes/No message box when the user exits the subform to ask if they want to print a report.
    If MsgBox("Do you want to print a report?, 4 + 32, "Print Report?") = 6 Then
    
    'Enter below what to do if they click 'Yes' (start printing)
    
     On Error GoTo ErrorHandler
        
        'To turn off screen repainting
        Application.Echo False
        
        DoCmd.OpenReport "rptReport", acViewPreview
        
        'Opens print dialog for current screen (report in this case):
        DoCmd.RunCommand acCmdPrint
        
    '<<<<<<<<Code stops here if Print Dialog is canceled>>>>>>>>>>>>>
    
        'To close the report after being sent to the printer
        DoCmd.Close acReport, "rptReport"
        
        'To turn on screen repainting
        Application.Echo True
    
    ErrorHandler:
        If Err.Number <> 0 And Err.Number <> 2501 Then
            MsgBox "Error: " & Err.Number & vbNewLine & Err.Description
            Exit Sub
        End If
    'Enter below what to do if they click 'No' (exit Sub)
    Else: 'MsgBox "They Clicked No"
        Exit Sub
    End If
    
    
    
    
    End Sub

  2. #2
    Jester0001 is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Feb 2012
    Location
    Indiana
    Posts
    69
    I believe I found a solution. When the print dialog is canceled, error 2501 is returned. Thus, the error handling in the previous code is set to only return errors that are not 0 and 2501 (...And Err.Number <> 2501). Thus, I added a little bit more error handling to the code to resume next on error 2501:

    Code:
    ErrorHandler:
        Select Case Err
          Case 2501
             Resume Next
          Case Else
    
       End Select
        If Err.Number <> 0 And Err.Number <> 2501 Then
            MsgBox "Error: " & Err.Number & vbNewLine & Err.Description
            Exit Sub
        End If
    'Enter below what to do if they click 'No' (exit Sub)
    Else: 'MsgBox "They Clicked No"
        Exit Sub
    End If
    
    End Sub

  3. #3
    ssanfu is offline Master of Nothing
    Windows 2K Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Well, first off, you are missing a closing double quote after the question mark in this line:
    Code:
    If MsgBox("Do you want to print a report?", 4 + 32, "Print Report?") = 6 Then
    It is much easier to read (IMO) if you use VBA constants:
    Code:
       If MsgBox("Do you want to print a report?", vbYesNo + vbQuestion, "Print Report?") = vbYes Then
    The on error statement is in the wrong place. it should be the line after
    "Private Sub subSubform_Exit(Cancel As Integer)"

    Then you have the "ELSE ... END IF" in the wrong place. It should be before the "ExitHere:" label. BTW, putting a colon ( : ) after the "Else" made it a label - shouldn't put a colon after the Else.

    The big question is why you set screen updating to FALSE, then open the report in preview mode??? You can't see the preview, so why open it??


    Why don't you just do this?
    Code:
    Private Sub subSubform_Exit(Cancel As Integer)
          On Error GoTo ErrorHandler
    
    'To show a Yes/No message box when the user exits the subform to ask if they want to print a report.
       If MsgBox("Do you want to print a report?", vbYesNo + vbQuestion, "Print Report?") = vbYes Then
    
          'Enter below what to do if they click 'Yes' (start printing)
    
          'Opens print dialog for current screen (report in this case):
          DoCmd.RunCommand acCmdPrint
    
       End If
    
    ExitHere:
       Exit Sub
    
    ErrorHandler:
       If Err.Number <> 0 And Err.Number <> 2501 Then
          MsgBox "Error: " & Err.Number & vbNewLine & Err.Description
       End If
       Resume ExitHere
    
    End Sub

  4. #4
    Jester0001 is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Feb 2012
    Location
    Indiana
    Posts
    69
    ssanfu,

    You asked very good questions! Thank you.

    First of all, this mess of code is what I get from copying/pasting stuff until it works (I'm full of shame!)

    The big question is why you set screen updating to FALSE, then open the report in preview mode??? You can't see the preview, so why open it??
    The code is run from a Form, and I want to print a Report. I disabled screen updating so that the user wouldn't see the report being opened, printed, then closed. The format of the Form is different than the format of the Report (is this bad design?) I tried your code, but it prints the Form, not the Report. Is there a way to print the Report from the Form without opening the Report, and still having the print dialog box open so the user can make selections if desired?

    Thanks!

  5. #5
    ssanfu is offline Master of Nothing
    Windows 2K Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Should have read my own code posting.. Sorry...


    Try this:
    Code:
    Private Sub subSubform_Exit(Cancel As Integer)
          On Error GoTo ErrorHandler
    
    'To show a Yes/No message box when the user exits the subform to ask if they want to print a report.
       If MsgBox("Do you want to print a report?", vbYesNo + vbQuestion, "Print Report?") = vbYes Then
    
          'Enter below what to do if they click 'Yes' (start printing)
    
          'Opens print dialog for report
          DoCmd.OpenReport "rptReport"
    
       End If
    
    ExitHere:
       Exit Sub
    
    ErrorHandler:
       If Err.Number <> 0 And Err.Number <> 2501 Then
          MsgBox "Error: " & Err.Number & vbNewLine & Err.Description
       End If
       Resume ExitHere
    
    End Sub
    From Help:
    acViewNormal prints the report immediately. If you leave this argument blank, the default constant (acViewNormal) is assumed.

    So you can use:
    DoCmd.OpenReport "rptReport",acViewNormal
    or
    DoCmd.OpenReport "rptReport"

    Both will result in the print dialog box being displayed to print the report.

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

Similar Threads

  1. Freeze or Lock data after querried
    By Dannat in forum Queries
    Replies: 1
    Last Post: 01-25-2012, 10:30 PM
  2. Freeze panes in reports
    By Jamy in forum Reports
    Replies: 4
    Last Post: 01-02-2012, 03:19 PM
  3. Freeze Columns in a Form?
    By Paul H in forum Forms
    Replies: 9
    Last Post: 09-09-2011, 01:40 PM
  4. Echo on/off
    By nigel brown in forum Access
    Replies: 3
    Last Post: 12-08-2010, 08:28 AM
  5. using VBA to freeze columns
    By bdaniel in forum Forms
    Replies: 1
    Last Post: 02-12-2010, 05:36 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