Online Book Reader

Home Category

Access Cookbook - Ken Getz [111]

By Root 2031 0
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.

See Also


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

5.2. Set and Retrieve the Name of the Default Output Device


Problem


Windows allows you to install a number of printer drivers, but one of them must always be denoted as the default printer. Although Windows provides its own concept of its default printer, Access maintains its own, independent default printer. You'd like to be able to control which printer Access thinks is the default printer, perhaps even choosing from a list of all the installed printers. Is there a way to do this from within Access?

Solution


Windows maintains its own list of available printers and stores information about the default printer. When Access starts up, it automatically uses Windows's default printer as its own default printer. Access's Application object provides a Printer property. Setting this property to refer to an item within the Printers collection allows you to control the default printer for all Access objects.

TIP

In Access, you always have the choice of printing an object to the default printer or to a specific printer. None of the techniques shown in this chapter that allow you to change the output destination will work if you set up your reports to print to a specific printer. In addition, printing to a specific printer will almost always lead to trouble if you distribute your applications to end users who may or may not have the same printer available. We suggest that, if possible, you set your reports so that they all print to the default printer.

To create a combo box in your own application that allows the user to choose a new default printer, follow these steps:

Add a combo box to your form and name it cboPrinters.

Add the following procedure to your form's module:

Private Sub FillPrinterList(ctl As Control)

' Fill the provided control (ctl) with a list of printers. This

' will cause a runtime error if ctl isn't a list or combo box.

Dim prt As Printer

ctl.RowSourceType = "Value List"

For Each prt In Application.Printers

ctl.AddItem prt.DeviceName

Next prt

End Sub

Modify the form's Open event procedure, so that it looks like this:

Private Sub Form_Load

Call FillPrinterList(Me.cboPrinters)

' Select the default printer, in the combo box.

' This may fail, so simply disregard errors.

On Error Resume Next

Me.cboPrinters = Application.Printer.DeviceName

End Sub

Modify the AfterUpdate event procedure of cboPrinters, so that it looks like this:

Private Sub cboPrinters_AfterUpdate( )

Dim lngIndex As Long

lngIndex = Me.cboPrinters.ListIndex

Set Application.Printer = Application.Printers(lngIndex)

End Sub

To see a sample application that allows you to select the default printer, load and run the form frmDefaultPrinterList from 05-02.MDB. This form, shown in Figure 5-2, includes a combo box from which you can select a new default printer for Access. When you first load the form, the combo box should already have the current default output device selected. If you make a choice, the code attached to the AfterUpdate event for the combo box will change the printer that Access uses for its default. This change will

Return Main Page Previous Page Next Page

®Online Book Reader