Access Cookbook - Ken Getz [110]
Place the following code in the form's Load event procedure (see the Preface for more information on creating event procedures):
Private Sub Form_Load( )
Dim prt As Printer
lstPrinters.RowSourceType = "Value List"
For Each prt In Application.Printers
lstPrinters.AddItem prt.DeviceName & " on " & prt.Port
Next prt
End Sub
To see an example of an application that lists a system's installed printers, load and run the form frmPrinterList from 05-01.MDB. Figure 5-1 shows the form displaying the installed printers on a test machine. Recipe 5.1.3 describes in detail the techniques used in building the list in Figure 5-1.
Figure 5-1. The sample form, frmPrinterList, showing the list of installed devices
Discussion
Access 2002 added several new objects that make working with printers and printing easier than these activities have been in the past. First, Access provides a Printers collection as a property of its Application object. This collection provides one Printer object corresponding to each output device installed on your computer. Each Printer object provides many properties, including DeviceName and Port, as used in this example.
Once you declare an object As Printer, you can iterate through the Printers collection, working with properties of each printer, like this:
Dim prt As Printer
For Each prt In Application.Printers
Debug.Print prt.DeviceName & " on " & prt.Port
Next prt
The Printer object provides a long list of available properties, each of which is discussed at some point in this chapter. The properties can be divided into three basic categories: printer name information (the DeviceName, Port, and DriverName properties); device capability information (e.g., the ColorMode, Copies, Duplex, Orientation, and PaperSize properties); and print layout information (e.g., the BottomMargin, ItemLayout, and RowSpacing properties). Later topics will discuss these groups of properties and will list and describe each property and its possible values. This example uses the DeviceName and Port properties, indicating the printer name and its associated output port.
There are several uses of the term "Printer" in Access. Access itself provides the Application.Printer object, which keeps track of the application's default printer. You can also declare and use a Printer object, which represents one entry in the application's Printers collection. Finally, each form and report in Access 2002 and later provides its own Printer object, which represents all the printer-related properties of the individual form or report. Later topics discuss how you can use each of these objects.
Adding items to a list box
The example form, frmPrinterList, adds information about each printer to a list box, using the new AddItem method of list and combo box controls. Access has historically provided three techniques for getting data into a list or combo box:
Binding the control to a table or query's output
Providing a delimited list of values in the control's RowSource property (and setting the RowSourceType property to "Value List")
Supplying an intricately crafted function (called a "list-filling callback function") that Access calls to retrieve data filling the list
None of these methods was perfect. The first option required you to have a table from which to retrieve the data. The second option limited your data source to a small number of characters (the size of the property has been increased a great deal starting in Access 2002, however). The third technique required advanced programming skills. None of the techniques made it easy to work with individual items in the lists.
Starting with Access 2002 this is all simpler. Modeled after the similar controls in Visual Basic, Access's list and combo boxes now support adding and removing individual items without needing to use any of the older techniques. Now you can add and remove items individually, as shown here. You can specify a location within the list where you'd like to