Procedure Name | Type | Description |
(Declarations) | Declarations | Declarations and private variables for the modPrinterAccess module. |
IsReportPortrait | Procedure | Determine if the report's orientation is portrait or landscape. |
SetReportOrientation | Procedure | Set a report's page orientation to Portrait or Landscape. Assumes the report can be modified. If it's already open in non-design mode (e.g. preview or report view) the report is closed. |
GetReportPrinterMargins | Procedure | Retrieve report printer margins in pixels. |
SetReportPrinterMargins | Procedure | Set report margin settings. |
IsReportPrinterDefault | Procedure | Determine if the report's current printer is the default printer. This is different from comparing the report's current printer to the default printer's name since it may be hard-coded to the current printer and would fail on a machine where the printer doesn't exist. |
ReportPrinter | Procedure | Retrieves the report's assigned printer. |
ResetReportPrinter | Procedure | Resets a report's printer to use the default printer. Assumes the report can be modified. If it's already open in non-design mode (e.g. preview or report view), the report is closed. This eliminates a report having a specified printer which may not exist on someone else's machine, causing this report to crash if printing is attempted. |
ResetReportPrinterAll | Procedure | Resets all the reports in the current database to use the default printer. This eliminates reports having a specified printer which may not exist on someone else's machine, causing the report to crash if printing is attempted. Assumes all reports are closed when this starts. |
IsFormPrinterDefault | Procedure | Determine if the form's current printer is the default printer. This is different from comparing the form's current printer to the default printer's name since it may be hard-coded to the current printer and would fail on a machine where the printer doesn't exist. |
FormPrinter | Procedure | Retrieves the form's assigned printer. |
ResetFormPrinter | Procedure | Resets a form's printer to use the default printer. This eliminates a form having a specified printer which may not exist on someone else's machine, causing this form to crash if printing is attempted. |
ResetFormPrinterAll | Procedure | Resets all forms to use the default printer. This eliminates forms from having a specified printer which may not exist on someone else's machine, causing the form to crash if printing is attempted. Assumes all reports are closed when this starts. |
' Example of modPrinterAccessReports ' ' To use this example, create a new module and paste this code into it. ' Then run the procedures by putting the cursor in the procedure and pressing: ' F5 to run it, or ' F8 to step through it line-by-line (see the Debug menu for more options) Private Sub Example_modPrinterAccessReports() ' Comments: Examples of managing the default printer, page orientation, and margins of Microsoft Access reports in Access VBA. ' This should be run from your Access database. Change the constants for the sample report to your report name. ' To see how the default printer gets reset, set a specific printer to your report: ' With your report in design mode, use the Page Setup ribbon, Page Setup, Page Tab, and choose a Specific Printer. ' See the results in the Immediate Window. Const cstrReport As String = "rptInvoice" Dim fDefaultPrinter As Boolean Dim strPrinter As String Dim fPortrait As Boolean Dim lngOriginalTop As Long, lngOriginalRight As Long, lngOriginalBottom As Long, lngOriginalLeft As Long Dim lngTop As Long, lngRight As Long, lngBottom As Long, lngLeft As Long Dim fChanged As Boolean ' Open the report in design mode and hidden so we can get information from it. ' This is helpful when invoking several of the functions for the same report and eliminates the need to open and close it each time. ' If you're only calling one of the functions, you can skip this since the function will automatically open and close the report if it isn't already open. DoCmd.OpenReport cstrReport, acViewDesign, , , acHidden ' Get report's printer margins Call GetReportPrinterMargins(cstrReport, lngOriginalTop, lngOriginalRight, lngOriginalBottom, lngOriginalLeft) Debug.Print "Original margins for report " & cstrReport Debug.Print " Margin-Top : " & lngOriginalTop Debug.Print " Margin-Right : " & lngOriginalRight Debug.Print " Margin-Bottom: " & lngOriginalBottom Debug.Print " Margin-Left : " & lngOriginalLeft ' Reduce margins by half Call SetReportPrinterMargins(cstrReport, lngOriginalTop / 2, lngOriginalRight / 2, lngOriginalBottom / 2, lngOriginalLeft / 2) ' Get the new margins Call GetReportPrinterMargins(cstrReport, lngTop, lngRight, lngBottom, lngLeft) Debug.Print "New margins" Debug.Print " Margin-Top : " & lngTop Debug.Print " Margin-Right : " & lngRight Debug.Print " Margin-Bottom: " & lngBottom Debug.Print " Margin-Left : " & lngLeft ' Reset to original margins Call SetReportPrinterMargins(cstrReport, lngOriginalTop, lngOriginalRight, lngOriginalBottom, lngOriginalLeft) Call GetReportPrinterMargins(cstrReport, lngTop, lngRight, lngBottom, lngLeft) Debug.Print "Reset margins to original values" Debug.Print " Margin-Top : " & lngTop Debug.Print " Margin-Right : " & lngRight Debug.Print " Margin-Bottom: " & lngBottom Debug.Print " Margin-Left : " & lngLeft Debug.Print ' ---------------------------- ' Check and adjust orientation fPortrait = IsReportPortrait(cstrReport) Debug.Print "Report orientation: " & IIf(fPortrait, "Portrait", "Landscape") If fPortrait Then fChanged = SetReportOrientation(cstrReport, acPRORLandscape) Else fChanged = SetReportOrientation(cstrReport, acPRORPortrait) End If Debug.Print "Report orientation was changed: " & fChanged If fChanged Then fPortrait = IsReportPortrait(cstrReport) Debug.Print "Report orientation is now: " & IIf(fPortrait, "Portrait", "Landscape") ' Change the orientation back to the original If fPortrait Then fChanged = SetReportOrientation(cstrReport, acPRORLandscape) Else fChanged = SetReportOrientation(cstrReport, acPRORPortrait) End If fPortrait = IsReportPortrait(cstrReport) Debug.Print "Report orientation is reset to: " & IIf(fPortrait, "Portrait", "Landscape") End If ' ------------------------- ' Work with default printer fDefaultPrinter = IsReportPrinterDefault(cstrReport) Debug.Print "The printer for report " & cstrReport & IIf(fDefaultPrinter, " is", " is not") & " the default printer" strPrinter = ReportPrinter(cstrReport) Debug.Print "The printer for report " & cstrReport & " is: " & strPrinter If Not fDefaultPrinter Then ' Reset the printer to the default printer if it's hard-coded to a specific printer fChanged = ResetReportPrinter(cstrReport) Debug.Print "Changed the printer for report " & cstrReport & " to the default printer: " & fChanged strPrinter = ReportPrinter(cstrReport) Debug.Print "The printer for report " & cstrReport & " is now: " & strPrinter End If DoCmd.Close acReport, cstrReport ' This can be used to reset all the reports in your database to the default printer: ' ResetReportPrinterAll End Sub Private Sub Example_modPrinterAccessForms() ' Comments: Examples of managing the default printer for Microsoft Access forms in Access VBA. ' This should be run from your Access database. Change the constants for the sample form to your form name. ' In general, you should use reports rather than forms for printing. ' See the results in the Immediate Window. Const cstrForm As String = "frmCatalog" Dim fDefaultPrinter As Boolean Dim strPrinter As String Dim fChanged As Boolean ' Open the form in design mode and hidden so we can get information from it. ' This is helpful when invoking several of the functions for the same form and eliminates the need to open and close it each time. ' If you're only calling one of the functions, you can skip this since the function will automatically open and close the form if it isn't already open. DoCmd.OpenForm cstrForm, acViewDesign, , , , acHidden ' ------------------------- ' Work with default printer fDefaultPrinter = IsFormPrinterDefault(cstrForm) Debug.Print "The printer for form " & cstrForm & IIf(fDefaultPrinter, " is", " is not") & " the default printer" strPrinter = FormPrinter(cstrForm) Debug.Print "The printer for form " & cstrForm & " is: " & strPrinter If Not fDefaultPrinter Then ' Reset the printer to the default printer if it's hard-coded to a specific printer fChanged = ResetFormPrinter(cstrForm) Debug.Print "Changed the printer for form " & cstrForm & " to the default printer: " & fChanged strPrinter = FormPrinter(cstrForm) Debug.Print "The printer for form " & cstrForm & " is now: " & strPrinter End If DoCmd.Close acForm, cstrForm ' This can be used to reset all the forms in your database to the default printer: 'ResetFormPrinterAll End Sub
The source code in Total Visual Sourcebook includes modules and classes for Microsoft Access, Visual Basic 6 (VB6), and Visual Basic for Applications (VBA) developers. Easily add this professionally written, tested, and documented royalty-free code into your applications to simplify your application development efforts.
Total Visual SourceBook is written for the needs of a developer using a source code library covering the many challenges you face. Countless developers over the years have told us they learned some or much of their development skills and tricks from our code. You can too!
Supports Access/Office 2016, 2013, 2010 and 2007, and Visual Basic 6.0!
"The code is exactly how I would like to write code and the algorithms used are very efficient and well-documented."
Van T. Dinh, Microsoft MVP