print report command button

purplecruz

New member
Local time
Today, 03:46
Joined
Apr 12, 2004
Messages
8
I have a command button that prints a report. When the user clicks the button, instead of automatically printing from the default printer, I would like the printer window to appear, so the user can choose which printer to use. Is there an easy way to do this?

Thanks purplecruz
 
Paste the following code into the form's code module:
Code:
Private Sub Command0_Click()
' Change [b]Command0[/b] to the actual name of the Command Button
' Example: if the Command Button is named [b]cmdPrint[/b], the above line would read:
' [b]Private Sub cmdPrint_Click()[/b]

On Error Resume Next

    DoCmd.OpenReport "Table1", acPreview
    ' Change [b]Table1[/b] to the actual name of the Report
    ' Example: if the report is named [b]MyReport[/b], the above line would read:
    ' [b]DoCmd.OpenReport "MyReport", acPreview[/b]

    DoCmd.SelectObject acReport, "Table1"
    DoCmd.RunCommand acCmdPrint
    If err.Number <> 0 And err.Number <> 2501 Then
        MsgBox err.Number & ":" & err.Description, vbCritical
    End If

End Sub
 
To show the Print dialogue box to allow you to select the printer you wish to print to do the following:

Set the code on the command button to

DoCmd.OpenReport "ReportName", acViewPreview

Add the following to the "On Activate" event of the report.

Private Sub Report_Activate()
On Error GoTo Err_Report_Activate

If Me.Report.HasData Then
DoCmd.RunCommand acCmdPrint
DoCmd.Close acReport, Me.Name

Else
MsgBox "There is no data for this report. Canceling report...", vbInformation
DoCmd.Close acReport, Me.Name

End If

Err_Report_Activate:
Resume Next
DoCmd.Close acReport, Me.Name

End sub
 
One more thing

Thanks ByteMyzer, that worked great.

The only problem is that when I push the print command button, the screen goes to the report. I want the the screen to keep the form displayed so that people don't have to go back and forth to the report and the form.
Thanks
purplecruz


ByteMyzer said:
Paste the following code into the form's code module:
Code:
Private Sub Command0_Click()
' Change [b]Command0[/b] to the actual name of the Command Button
' Example: if the Command Button is named [b]cmdPrint[/b], the above line would read:
' [b]Private Sub cmdPrint_Click()[/b]

On Error Resume Next

    DoCmd.OpenReport "Table1", acPreview
    ' Change [b]Table1[/b] to the actual name of the Report
    ' Example: if the report is named [b]MyReport[/b], the above line would read:
    ' [b]DoCmd.OpenReport "MyReport", acPreview[/b]

    DoCmd.SelectObject acReport, "Table1"
    DoCmd.RunCommand acCmdPrint
    If err.Number <> 0 And err.Number <> 2501 Then
        MsgBox err.Number & ":" & err.Description, vbCritical
    End If

End Sub
 
Did you try the code I posted ?
It will do what you want.

Dave
 
Hi,

I've the same problem and it works now after trying the code given there. Good work.

But one thing i'd like to ask is that i've got "run-time error '2501'. the runcommand action was cancelled". This happen when i click on cancel instead of printing. Is there a way to get around this by catching this error and hide it.

Thank you in advance
 
2 ways to solve this problem.

1..(the one I use) remove the error handling routine and after the sub call put
On Error Resume Next


2.. In the error handler routine put

If Err <> 2501 then
Msgbox Err.Description
Resume Exit ..... what ever
end if

HTH
Dave
 

Users who are viewing this thread

Back
Top Bottom