Online Book Reader

Home Category

Access Cookbook - Ken Getz [119]

By Root 2014 0
printer, you won't have the option of changing the default Access printer.

Discussion


Previous 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.

5.8. Find Which Reports Are Not Set to Print to the Default Printer


Problem


You are about to distribute your application to other Access users. You want to ensure that all your reports are set to print to the default printer so that they will work with the users' installations of Windows. How do you create a list of all your reports and show whether or not they have been saved with the default printer setting?

Solution


Building on the code examples in this chapter, you can investigate the UseDefaultPrinter property of each report to determine if it has the default printer selected. This solution uses this property, along with some simple ActiveX Data Objects (ADO) code, to get a list of reports in your database, to check the default printer setting, and to save the results to a table. This table feeds a report that you can print,

Return Main Page Previous Page Next Page

®Online Book Reader