Access Cookbook - Ken Getz [117]
Set rpt = Reports(strReport)
Then set the PaperBin property of the report's Printer object, select the report, and print the first page, like this:
rpt.Printer.PaperBin = FirstPagePaperBin
DoCmd.SelectObject acReport, strReport
DoCmd.PrintOut acPages, 1, 1
Set the PaperBin property for the rest of the pages, and print them (the report is already selected, so you don't need to select it again):
rpt.Printer.PaperBin = AllPagesPaperBin
' Print all the rest of the pages (up to 32000).
DoCmd.PrintOut acPages, 2, 32000
The PrintOut method's implementation is somewhat unfortunate. It's the only way you can control the specific pages you want printed, yet it requires you to select the object to be printed before printing it. This combination of requirements means that you must first open the report in preview or design view and set its properties, then select and print it. You cannot select a hidden report (Access will unhide it before selecting it), so your best bet is to open it with the WindowMode parameter of the DoCmd.OpenReport method set to acIcon. That way, the report is minimized. If this behavior truly bothers you, you can use Application.Echo to turn off screen display before you open the report and then turn it back on when you're done. In addition, if you specify the first page to be printed, you must also specify the last page. Therefore, even if you don't know how many pages your report contains, you must specify an upper bound (32,000, in our example) when you print. Hopefully, your report won't contain more than 32,000 pages. If it does, bump up that number.
If you're going to provide this functionality in an application to be distributed to users who have printers on which it hasn't been tested, you'll need to make it clear that some of the bins listed in the combo boxes may not work with their printers. It may require some experimentation on their part to determine which settings are correct.
5.6. Retrieve Information About a Report or Form's Selected Printer
Problem
Access's File → Page Setup dialog allows you to specify either the default printer or a specific printer for each printable object. You'd like to be able to find out, programmatically, which printer has been selected for an object and whether the object is set to print to the default printer. How can you retrieve that information?
Solution
In addition to the properties you've seen so far, the Printer object keeps track of the three pieces of information that Windows must know about an output device: the device name (for example, "HP LaserJet 4"), the driver name ("WINSPOOL"), and the output port ("LPT1:"). Access also keeps track of whether the report has been set to print to the default printer or to a specific printer, in the UseDefaultPrinter property of the report. You'll use these properties to determine the information you need.
Load and run the form frmSelectedPrinters in 05-06.MDB. Figure 5-6 shows the form after rptReport3 is selected and the report's output device, driver, and port are filled in on the form. Because this report was set up to print to the default printer, the "Printing to Default Printer" checkbox is selected.
Figure 5-6. frmSelectedPrinters, after selecting rptReport3
The sample form uses this code to do its work:
Private Sub cboReportList_AfterUpdate( )
Dim strReport As String
Dim rpt As Report
On Error GoTo HandleErrors
strReport = Me.cboReportList
DoCmd.OpenReport strReport, View:=acViewPreview, WindowMode:=acHidden
With Reports(strReport)
With .Printer
Me.txtDevice = .DeviceName
Me.txtDriver = .DriverName
Me.txtPort = .Port
End With
Me.chkDefault = .UseDefaultPrinter
End With
ExitHere:
DoCmd.Close acReport, strReport
Exit Sub
HandleErrors:
MsgBox "Error: " & Error & " (" & Err & ")"
Resume ExitHere
End Sub
To retrieve printer information about forms or reports in your own applications, follow these steps:
Open the selected report in either preview or design view:
DoCmd.OpenReport strReport, View:=acViewPreview, WindowMode:=acHidden