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?
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.
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 affect any printing you do from within Access, if the printed object has been set up to print to the default printer.
You saw the code in the FillPrinterList procedure in the previous section. This time it's separated out into its own procedure, so it will be easier for you to use in future projects. The form's Load event procedure fills the combo box with its list of printers and then attempts to set the value of the combo box to match the name of the currently selected default printer, using this code:
Me.cboPrinters = Application.Printer.DeviceName
After you select an item from the list of printers in the combo box, the AfterUpdate event procedure for that control sets the application's default printer to the printer you just selected, using this code:
Set Application.Printer = Application.Printers(lngIndex)
Access uses the default output device for printing, unless you specify otherwise in Access's File Page Setup dialog. The Solution in Recipe 5.7 will combine methods from this section and others to show you how to send a report to the printer you choose at runtime. The methods shown there allow you to direct a report to the printer one day and to the fax modem the next.
If you change Access's default printer and then determine that you'd like to put it back to its original value, you can. Simply set the Application.Printer value to the built-in value Nothing, and Access will revert to its original default printer:
Set Application.Printer = Nothing
Although setting the value to Nothing instead of to a specific printer seems odd, Access understands the special value and simply reverts to the original default printer it found in Windows when it started the current session.
Although you could use the Windows API to change Windows's understanding of its own default printer, there's really no need to do that from within Access. Access maintains its own default printer (assuming the default printer from Windows when it first starts a session), and you can easily manipulate that value using the techniques shown here.