Access Cookbook - Ken Getz [114]
Dim strChosen As String
Dim rpt As Report
strChosen = Me.cboReportList
With Reports(strChosen).Printer
.LeftMargin = FromInches(Me.txtLeft)
.RightMargin = FromInches(Me.txtRight)
.TopMargin = FromInches(Me.txtTop)
.BottomMargin = FromInches(Me.txtBottom)
.DataOnly = Me.chkDataOnly
.DefaultSize = Me.chkDefaultSize
If Not .DefaultSize Then
.ItemSizeWidth = FromInches(Me.txtXFormSize)
.ItemSizeHeight = FromInches(Me.txtYFormSize)
End If
.ItemsAcross = Val(Me.txtCxColumns)
.RowSpacing = FromInches(Me.txtYFormSpacing)
.ColumnSpacing = FromInches(Me.txtXFormSpacing)
.ItemLayout = Me.grpRadItemOrder
End With
The combo box containing the list of reports uses a common but undocumented technique. The Access system tables (check Tools → Options → View → System Objects to see the system tables in the database container) contain information about the current database. One table in particular, MSysObjects, contains a row for each object in the database. To fill the combo box with a list of reports, you can use this SQL expression:
SELECT Name FROM MSysObjects WHERE Type = -32764 ORDER BY Name;
The Name column includes the name for each object, and the Type column contains -32764 for reports (or -32768 for forms). Microsoft suggests using DAO or ADO instead of querying against the system tables to retrieve lists of items; however, our method is much faster and much simpler for filling lists. This method has worked in every version of Access so far; we can only assume it will continue to do so.
The Printer object provides one more bit of unexpected behavior: unless you've set the DefaultSize property to False, you cannot set the ItemSizeWidth or ItemSizeHeight properties—you'll trigger a runtime error if you try. The sample code determines the value in the DefaultSize property and attempts to change the other two properties only if doing so won't cause an error.
5.4. Programmatically Change Printer Options
Problem
You've tried using SendKeys to change printing options in the File → Setup Page dialog, but this really isn't satisfactory. Sometimes it works and sometimes it doesn't, depending on the circumstances and the printer driver that's loaded. Is there some way to modify printer options without using SendKeys?
Solution
Windows makes many of the printer driver settings available to applications, including the number of copies, page orientation, and page size. Starting with Access 2002, it's easy to retrieve and modify these values, using the Printer property of forms and reports. This solution focuses on the print settings features of the Printer object and demonstrates how to read and write values in the Printer object.
To be able to modify printer settings for reports or forms in your own applications, follow these steps:
Open the report in either preview or design view. (If you want to make your changes persistent, open the report in design view. If you want to apply changes for just this particular instance, open it in preview mode.)
Modify some of the properties of the Printer object provided by your form or report. For example, the sample form works with a small subset of the available printer-specific properties, using code like this:
DoCmd.OpenReport strReport, View:=acViewPreview
With Reports(strReport).Printer
Me.txtCopies = .Copies
Me.grpOrientation = .Orientation
Me.grpPaperSize = .PaperSize
End With
When you're done working with the properties, write them back to the report's Printer object, using code like this (from the sample form):
strReport = Me.cboReportList
With Reports(strReport).Printer
.Copies = Me.txtCopies
.Orientation = Me.grpOrientation
.PaperSize = Me.grpPaperSize
End With
For an example, load and run the form frmPrintSettings in 05-04.MDB. Figure 5-4 shows the sample form in action. This form allows you to choose a report from a combo box. Once you've made your choice, the form loads the report in preview mode and