Access Cookbook - Ken Getz [112]
Figure 5-2. frmDefaultPrinterList allows you to choose a new Access default printer
Discussion
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.
5.3. Programmatically Change Margin and Column Settings for Reports
Problem
You'd like to give your applications' users some control over report layout, especially in designating column and margin settings. You could just let them loose in report design mode, but you'd like to maintain a little control over their actions. Is there some way to modify these layout settings from VBA?
Solution
Starting with Access 2002, each form and report object includes a Printer property. Retrieving this property gets you an object with many properties, several of which deal with margin and column settings for reports.
You can use properties of a report's Printer object to retrieve and set layout properties. You'll find properties representing the left, top, bottom, and right margins; the number of columns; and the size, spacing, and item order of the columns. In addition, the Printer object contains the Data Only option in the File → Page Setup dialog. This solution demonstrates how to use the print layout properties provided by the Printer object.
Load and run the form frmPrintSettings from 05-03.MDB. Figure 5-3 shows the form (which emulates Access's File → Page Setup dialog) after the report Report1 has been selected from the list of reports. Choose a report from the drop-down list, and the form will load that report in preview mode. You can change the settings for the selected report by typing new values into the text boxes. To save the changes to the selected report, click on Save Settings. You'll see those changes in the preview window.
Figure 5-3. frmPrintSettings provides the same functionality as the Access File → Page Setup dialog
Some of the items on the form are available only if you've specified more than one column for the Items Across value, so you'll want to use a number greater than 1 in that field. (Because this example opens the report in preview mode, changes you