Controlling the Printer
Prior to Access 2002, there was no easy way to programmatically control the printer in the applications that you built. Unlike other aspects of Access in which Microsoft provided you with objects, properties, methods, and events that you could easily manipulate, programmatically controlling the printer in versions prior to Access 2002 involved rolling up your sleeves, and talking at a low level to operating system objects.Fortunately, Access 2002 introduced a new Printer object and a Printers collection. The Printer object greatly facilitates the process of programmatically manipulating a printer. The Printers collection allows you to loop through all the Printer objects and perform a task.
The Printer Object
The Printers collection consists of individual Printer objects. You use a Printer object to control each printer in the Printers collection. Listing 10.13 provides an example of the Printer object.
Listing 10.13 The Printer Object
Private Sub cmdPrinterObject_Click()
'Declare a Printer object
Dim prt As Printer
'Point the Printer object at the first printer in
'the Printers collection
Set prt = Printers(0)
'Display properties of the printer
MsgBox "Device Name: " & prt.DeviceName & vbCrLf & _
"Port: " & prt.Port & vbCrLf & _
"Color Mode: " & prt.ColorMode & vbCrLf & _
"Copies: " & prt.Copies
End Sub
Listing 10.13 begins by instantiating a Printer object. It points the Printer object at the first printer in the Printers collection. It then retrieves the DeviceName, Port, ColorMode, and Copies properties of the printer. These are four of the many properties included for the Printer object. Other properties include the LeftMargin, RightMargin, TopMargin, BottomMargin, Orientation, and PrintQuality properties. Most properties of the Printer object are read/write. This means that you can programmatically manipulate the properties at runtime, easily controlling the behavior of the printer.
The Printers Collection
Using the Printers collection, you can loop through all the printers available for a user, programmatically manipulating each one. Listing 10.14 provides an example. It is found in frmPrinterObjectAndPrintersCollection on the sample code CD.
Listing 10.14 The Printers Collection
Private Sub cmdPrintersCollection_Click()
'Declare a Printer object
Dim prt As Printer
Dim strPrinterInfo As String
'Loop through each printer in the user's
'Printers collection
For Each prt In Printers
'Retrieve properties of the printer
strPrinterInfo = strPrinterInfo & vbCrLf & _
"Device Name: " & prt.DeviceName & "; " & _
"Port: " & prt.Port & "; " & _
"Color Mode: " & prt.ColorMode & "; " & _
"Copies: " & prt.Copies
Next prt
'Display the properties of all printers in a
'message box
MsgBox strPrinterInfo
End Sub