Problem printing report - preview

rick982

New member
Local time
Today, 12:26
Joined
Feb 12, 2009
Messages
7
I have searched for an answer to this question without any sucess, so any help would be appreciated.

I have a report that is linked to a command button on a form. I have the following VBA code on the button:

Private Sub cboRMAComplete_Click()
DoCmd.Close
DoCmd.OpenReport "RMA Label", acViewPreview
DoCmd.RunCommand acCmdPrint
End Sub

All this works, it brings up a blank preview of the report (not an issue as I don't care whether the users even see the preview or not), the Print Dialog box is opened over the preview. It allows me to select a specific printer and prints the report correctly. All good so far.

Now the problem: Once you select the printer and print the report, the print dialog box closes, and a preview of the report appears in the report window. The only way to close the report window is by clicking on the X on the window. What I need to do is get the report window to close programmatically as some of the users of this application are frankly not competent enough to be trusted to click an X to close the window. I have code also that runs on the close report event that is necessary to the operation of the database as well, which is why I do not want to leave it to chance as far as closing the window.

Any ideas?
 
Thanks for the response scalextric59. Got it figured out from that. Bit of a blonde moment I think :)
 
to go that little bit further, you can also code to close the report if you want to press 'cancel' on the print dialog. here's the full code i use for one of my reports custom 'print' button (closing form on cancel is handled by the error handler, i've highlighted this in red):

Code:
Private Sub cmdHorsePrint_Click()
[COLOR=Red]On Error GoTo Err_cmdHorsePrint_Click[/COLOR]

    Dim stDocName As String
    Dim strCriteria As String
    
    stDocName = "rptHorsePersonal"
    strCriteria = "[ID]= " & Me![ID]
    
    DoCmd.RunCommand acCmdSaveRecord
    DoCmd.OpenReport stDocName, acPreview, , strCriteria, , Me![HorseName]
    DoCmd.RunCommand acCmdPrint
    DoCmd.Close acReport, stDocName, acSaveNo

[COLOR=Red]Exit_cmdHorsePrint_Click:
    Exit Sub[/COLOR]

[COLOR=Red]Err_cmdHorsePrint_Click:
    Select Case Err.Number
      Case 2501
        'command cancelled
        DoCmd.Close acReport, stDocName, acSaveNo
      Case Else
        Msg = "Error # " & Str(Err.Number) & Chr(13) & Err.Description
        MsgBox Msg, vbOKOnly, "The PED", Err.HelpFile, Err.HelpContext
    End Select
    Resume Exit_cmdHorsePrint_Click
[/COLOR]    
End Sub
 

Users who are viewing this thread

Back
Top Bottom