[ Team LiB ] |
Recipe 5.7 Choose an Output Device at Runtime5.7.1 ProblemYou'd like to be able to select an output device while your application is running without popping up the File Page Setup dialog. Is there a way to present a list of available printers and have the chosen report print to the chosen device? For example, you want to print your reports to the printer sometimes and sometimes to the fax machine. 5.7.2 SolutionThough this topic sounds complex, its solution is really just a combination of other solutions in this chapter. The Solution in Recipe 5.2 shows how to retrieve a list of available print devices and retrieve and set the default device. The Solution in Recipe 5.6 shows how to determine if a given report or form is configured to print to the default printer. Given those two techniques, this solution shows you how to set a new output device, print the Access object (using the new default device), and then restore the original default device. Starting with Access 2002, you'll find two ways in which you can change the output device: you can either change Access's default printer, then print your report to the new default printer; or you can simply change the report's selected output device. The first solution is easier and generally works better. The second requires an extra step (selecting the report on screen) but gives you more flexibility. Load and run frmDefaultPrinterList from 05-07.MDB. Figure 5-7 shows the form in use, with the report rptReport3 selected and ready to print. Because rptReport3 has been configured to print to the default printer (you can open the File Page Setup dialog to confirm this), the "Print to Default Printer" checkbox on the sample form is checked. You can choose a different output device from the combo box on the bottom of this form (of course, this will be interesting only if you happen to have more than one output device installed). If you choose a different output device (a fax driver, for example), the sample form will send the selected report to that output device. Figure 5-7. frmDefaultPrinterList, ready to choose a new output deviceThe sample form also includes a checkbox ("Change Default Printer") that is available only if your selected report has been set up to print to the default printer. If so, you can elect to either change the default printer or select a new printer for the report. If the report has been designed so that it prints to a specific printer, you won't have the option of changing the default Access printer. 5.7.3 DiscussionPrevious topics have discussed all but one of the issues demonstrated in this demo. The only outstanding issue is the code for printing the report (setting the new printer, printing the report to the new printer, and then resetting the original device). When you click "Print to Chosen Destination" on the sample form, you execute the following code in the form's module: Private Sub cmdChosen_Click( ) On Error Resume Next Dim strRptName As String strRptName = cboObjects.Value If chkChangeDefaultPrinter.Value Then Set Application.Printer = Application.Printers(cboDestination.ListIndex) DoCmd.OpenReport strRptName, View:=acViewNormal Set Application.Printer = Nothing Else DoCmd.OpenReport strRptName, View:=acPreview, WindowMode:=acHidden With Reports(strRptName) Set .Printer = Application.Printers(cboDestination.ListIndex) End With DoCmd.OpenReport strRptName, View:=acViewNormal End If End Sub This code takes two different paths, depending on the value of the "Change Default Printer" checkbox. If it's selected, the code sets the default printer to the printer you selected in the combo box on the form, then prints the report. Finally, it sets the Application.Printer property to Nothing, resetting it back to its original value: Set Application.Printer = Application.Printers(cboDestination.ListIndex) DoCmd.OpenReport strRptName, View:=acViewNormal Set Application.Printer = Nothing If you didn't select the checkbox, you chose not to modify the default printer but instead to modify the report's internal printer. In this case, the code opens the report hidden, sets the Printer property of the report to be the report you've selected, then opens the report again, this time in normal view (causing it to be printed): DoCmd.OpenReport strRptName, View:=acPreview, WindowMode:=acHidden With Reports(strRptName) Set .Printer = Application.Printers(cboDestination.ListIndex) End With DoCmd.OpenReport strRptName, View:=acViewNormal If you click "Print to Current Destination", the form sends the report to its currently selected printer by simply calling the DoCmd.OpenReport method. You can extract from this example just the code you need for your own situation. If you want to modify the default Access printer, use the first code fragment. If you want to change the report's printer (leaving the Access printer intact), use the second fragment. You can make many changes to this sample application. You might, for example, want to supply the report name without providing a combo box for it on the form. In that case, you would use a form like the sample form in the Solution in Recipe 5.2, showing only the list of output devices. You would modify the procedure described in this section to take the report name from a variable instead of from the form's combo box. It's unfortunate that you cannot modify the output device from within the report's Open event. If that was possible, you could avoid opening the report first in preview or design view, setting its Printer property, and then printing the report. For the most part, you'll be better off simply changing Access's default printer. |
[ Team LiB ] |