|[ Team LiB ]|
Recipe 5.1 Retrieve a List of All the Installed Output Devices
Access makes it easy to fill a list or combo box with all the available printers. You can iterate through the members of the Application.Printers collection, retrieving the DeviceName and Port of each printer.
To create a list of installed printers, follow these steps:
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.
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.
18.104.22.168 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:
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 add an item, like this:
' Add the new items at the top of the list. lstPrinters.AddItem prt.DeviceName & " on " & prt.Port, 0
You can also add items to the end of the list by simply not specifying the location for the new item. To remove items from the list, call the RemoveItem method, specifying the index (starting at 0) of the item you'd like to remove.
If you want to add items to a list or combo box, you must set the RowSourceType property of the control to "Value List" before you start. Although you could set that property while laying out your form, we like to set these "make or break" properties as forms load. If you set one of these properties incorrectly, the form won't load—setting the property in code ensures that the form will load correctly. This is a matter of style, but it never hurts to make sure important properties are set properly in code.
5.1.4 See Also
|[ Team LiB ]|