Access Cookbook, 2nd Edition [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

Access Cookbook, 2nd Edition [Electronic resources] - نسخه متنی

Ken Getz; Paul Litwin; Andy Baron

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
لیست موضوعات
توضیحات
افزودن یادداشت جدید










Recipe 5.1 Retrieve a List of All the Installed Output Devices



5.1.1 Problem


You'd like to be
able to present your users with a list of all the installed printers.
Is there some way to fill a list box with the printer names?


5.1.2 Solution


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:

  1. Add a list box to a form. Set the Name property of the list box to
    lstPrinters.

  2. 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



5.1.3 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.


5.1.3.1 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 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 loadsetting 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


For more information on working with list and combo boxes, see
Recipe 7.5 in Chapter 7.


/ 232