Online Book Reader

Home Category

Access Cookbook - Ken Getz [116]

By Root 1784 0
bin

acPRBNLargeFmt

10

Large-format bin

acPRBNLower

2

Lower bin

acPRBNManual

4

Manual bin

acPRBNMiddle

3

Middle bin

acPRBNSmallFmt

9

Small-format bin

acPRBNTractor

8

Tractor bin

acPRBNUpper

1

Upper bin

acPRBNFormSource

15

Form source

The sample form opens reports in preview mode and allows you to modify and view printer-specific properties, then print the report. If you want to modify the design properties for a report, you'll need to open it in design view, modifying the call to the DoCmd.OpenReport method in the code, like this:

DoCmd.OpenReport strReport, View:=acViewPreview

When you're done, save the report using code like this:

DoCmd.Close acReport, "YourReportName", acSaveYes

Although Access makes it easy to work with printer settings, the Printer object is missing some important features. For example, although you can select acPRPSUser for the PaperSize property, you cannot define your own sizes (making this option effectively useless).

5.5. Programmatically Control the Paper Source


Problem


You'd like to be able to print the first page of your reports from a paper tray containing letterhead paper and then print the rest on normal paper stock. Is there some way in Access to switch paper trays programmatically from within your application?

Solution


The paper source is one of the properties of the Printer object associated with a report (see the Solution in Recipe 5.4 for a description of the Printer object) that you can programmatically control. Given the information in the Solution in Recipe 5.4, it's relatively easy to change the paper source for a report so that the first page prints from one paper bin and the rest prints from another.

Load and run frmPaperSource in 05-05.MDB (Figure 5-5).

Figure 5-5. frmPaperSource allows you to print from different paper sources

With frmPaperSource loaded, choose a report. The report will load, minimized, in preview mode.

Choose a paper bin for the first page and a bin for the rest of the pages. Note that the lists of paper bins contain all the possible paper sources; your printer may not support all of the options listed in the combo boxes. You'll need to find the bins that work correctly with your printer driver.

Click the Print button. Access should print the first page of the report from the bin chosen for the first page and the rest from the bin chosen for the other pages.

To use this technique in your own applications, you'll need to add code that supports printing the first page, then the rest of the pages, as the result of some action (such as clicking a command button). In reaction to this event, call the PrintPages procedure, shown here:

Private Sub PrintPages(strReport As String, _

FirstPagePaperBin As AcPrintPaperBin, _

AllPagesPaperBin As AcPrintPaperBin)

Dim rpt As Report

On Error GoTo HandleErrors

DoCmd.OpenReport strReport, acViewPreview, WindowMode:=acIcon

Set rpt = Reports(strReport)

rpt.Printer.PaperBin = FirstPagePaperBin

' Unfortunately, you have to select the report in order to print it.

' Who wrote the PrintOut method this way, anyway?

DoCmd.SelectObject acReport, strReport

DoCmd.PrintOut acPages, 1, 1

' Define the paper source.

rpt.Printer.PaperBin = AllPagesPaperBin

' Print all the rest of the pages (up to 32000).

DoCmd.PrintOut acPages, 2, 32000

ExitHere:

DoCmd.Close acReport, strReport, acSaveNo

Exit Sub

HandleErrors:

MsgBox "Error: " & Err.Description & " (" & Err.Number & ")"

Resume ExitHere

End Sub

In the sample form, this code is called from the Click event of the Print button, like this:

Private Sub cmdPrint_Click( )

Call PrintPages(Me.cboReportList, Me.cboFirstPage, Me.cboAllOther)

End Sub

Discussion


As you saw in the Solution in Recipe 5.4, you can use a form or report's Printer property to change its paper source. Printing one page of a report from one bin and the rest from another is easy. First, open the report in preview mode and get a reference to the open report:

DoCmd.OpenReport strReport, acViewPreview,

Return Main Page Previous Page Next Page

®Online Book Reader