Using the Printer Object in Code

3 4

The new Printers collection and Printer object in the Access object model address a long-standing need: They let you programmatically select a printer for a report and also change the paper size and orientation.

caution


Changing the paper size or orientation of a report doesn’t change the report width or the arrangement of controls on the report; only the detail section height is affected when you select a different paper size or orientation. This means that if you select legal landscape paper for a letter-portrait formatted report, it will look very strange indeed.

By using a printer selection combo box powered by the Printer object, you let users select any available printer for printing a report without having to open the report, change the printer or other settings, and save and close the report—a great convenience. Although you can’t change the dimensions or layout of reports using the Printer object, you can at least try different selections until you find the right one for the report you’re printing—for example, if you don’t know whether the report is designed to be printed portrait or landscape.

Figure 20-24 shows a dialog form with combo boxes for selecting a report for printing, a printer, and the paper size to use; it also has an option group for selecting the orientation. This form is available on the companion CD in the Test Access 2002.mdb database.

Figure 20-24. The Select Printer For Report dialog box is an example of a form that uses the <i>Printer</i> object.

Figure 20-24. The Select Printer For Report dialog box is an example of a form that uses the Printer object.

The cboSelectReport combo box has tlkpReports as its row source, which is a table of reports in the database created by the Menu Manager add-in for use as the row source of a report selection combo box on the main menu. The cboSelectPrinter combo box has tlkpPaperSizes as its row source, which is a table listing all the values of the AcPrintPaperSize enumeration and is used to set the PaperSize property of the Printer object.

The cboSelectPrinter combo box is filled with values from the form’s Load event procedure; the names of the printers in the Printers collection are added to the list using the new AddItem method of the Listbox object. The fraOrientation option group offers two options: Portrait and Landscape. After you select the options you want, you can click the Preview Report button to open the report in Print Preview, with the selected paper size and orientation.

The code for the fdlgSelectPrinter form is shown here:

 Option Compare Database Option Explicit Dim intDefaultPrinter As Integer Private Sub cmdPrintReport_Click() On Error GoTo ErrorHandler    Dim prt As Printer    Dim strReport As String        Set prt = Application.Printers(CLng(Me![cboSelectPrinter]))    strReport = Me![cboSelectReport]    prt.PaperSize = Me![cboPaperSize]    prt.Orientation = Me![fraOrientation]    DoCmd.OpenReport strReport, acViewPreview    Reports(strReport).Printer = prt     ErrorHandlerExit:    Exit Sub ErrorHandler:    MsgBox "Error No: " & Err.Number & "; Description: " & _        Err.Description    Resume ErrorHandlerExit End Sub Private Sub Form_Load() On Error GoTo ErrorHandler         Dim prt As Access.Printer    Dim i As Integer    Dim cboPrinter As Access.ComboBox        DoCmd.RunCommand acCmdSizeToFitForm    Set cboPrinter = Me![cboSelectPrinter]    cboPrinter.RowSource = ""    i = 0    For Each prt In Application.Printers       cboPrinter.AddItem i & ";" & prt.DeviceName       If prt.DeviceName = Application.Printer.DeviceName Then          intDefaultPrinter = i       End If       i = i + 1    Next    cboPrinter = intDefaultPrinter    Me![cboPaperSize] = 1 ErrorHandlerExit:    Exit Sub ErrorHandler:    MsgBox "Error No: " & Err.Number & "; Description: " & _        Err.Description    Resume ErrorHandlerExit End Sub 



Microsoft Access Version 2002 Inside Out
Microsoft Access Version 2002 Inside Out (Inside Out (Microsoft))
ISBN: 0735612838
EAN: 2147483647
Year: 2005
Pages: 172
Authors: Helen Feddema

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net