Recipe 5.6 Retrieve Information About a Report or Form's Selected Printer
5.6.1 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?
5.6.2 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
- Use the DeviceName,
DriverName, and Port properties of the report's
Printer property to retrieve information about the
report's output location:With Reports(strReport)
With .Printer
Me.txtDevice = .DeviceName
Me.txtDriver = .DriverName
Me.txtPort = .Port
' Code removed.
End With
End With - Check the
report's UseDefaultPrinter property to determine if
the report has been set to print to the default printer:With Reports(strReport)
' Code removed.
Me.chkDefault = .UseDefaultPrinter
End With
The UseDefaultPrinter property hangs off of the report itself, while
the rest of the properties discussed in this chapter are members of
the Printer object returned by the report's Printer
property. You may look for the UseDefaultPrinter property in the
wrong placeremember, it's a property of the
report.The UseDefaultPrinter property becomes more important, as
you'll see in the next section, when you want to
change the output device at runtime. Because of the way the Printer
object was designed, you cannot change the output device from the
Open event of the reportyou must change it from outside the
report. The easiest way to do this is to change
Access's default printer, then print the report,
then put Access's default printer back to what it
was. You can accomplish this only if the report has been set to print
to Access's default printer. You can look at the
UseDefaultPrinter property to determine if that's
how the report was set up. (You cannot, however, change a
report's UseDefaultPrinter property if
it's open in preview or print modeyou can
change it only when you've opened the report in
design view.)