Introduction
When an MS-Access Application is installed on a network, security is one of the major issues that the Database Developer has to tackle. This includes the security of data and objects within the Database and the Database file itself. To learn more about securing a database on a Network, visit the following link:
We’re now going to address another common issue often faced by users, which is usually resolved temporarily through alternative methods. When MS Access reports are designed for a specific printer on the network and all users share that printer, there are generally no problems. However, if users attempt to print the report on a different printer—either another network printer or a local printer—the report may not print correctly. Differences in default paper size, page orientation, or margin settings between printers can cause the report layout to appear incorrect when printed.
To work around this issue, users typically need to preview the report, open the Page Setup menu if necessary, and manually adjust the paper size, page orientation (portrait or landscape), and margins before printing. This option is only available if the Report Page Setup command is accessible to the user. If the application uses customized menus and toolbars, this option may not be visible, complicating the process further.
For more details on Customized Menus and Toolbars, visit the following Links:
Reports PrtDevMode Property
- Orientation - Valid Values: 1 = Portrait, 2 = Landscape.
- PaperSize 9 = A4 (210 x 297 mm)
To make printing easier for users, we can modify the PrtDevMode property of a report through a program to automatically adjust critical parameters—such as paper size, page orientation (portrait or landscape), and margins—before sending the report to the printer. This ensures that the report prints correctly on any printer.
The PrtDevMode property is a 94-byte structure containing multiple parameters that can be modified via code to control how the printer behaves.
For this example, we’ll focus on two simple parameters for our report. Our sample report is designed in landscape mode and is to be printed on A4 paper (210 × 297 mm). We must modify the following member parameters of the PrtDevMode property of the default printer:
Working with the Report.PrtDevMode Property Values
The above options (Orientation and Paper size) appear on the Page tab in the Page Setup Dialog box in the File Menu. We are trying to change these values at run-time through the Program.
Open a new Standard Module (Global Module) in your Database and copy the following code into the module and save it.
Private Type str_DEVMODE
RGB As String * 94
End Type
Private Type type_DEVMODE
strDeviceName As String * 16
intSpecVersion As Integer
intDriverVersion As Integer
intSize As Integer
intDriverExtra As Integer
lngFields As Long
intOrientation As Integer
intPaperSize As Integer
intPaperLength As Integer
intPaperWidth As Integer
intScale As Integer
intCopies As Integer
intDefaultSource As Integer
intPrintQuality As Integer
intColor As Integer
intDuplex As Integer
intResolution As Integer
intTTOption As Integer
intCollate As Integer
strFormName As String * 16
lngPad As Long
lngBits As Long
lngPW As Long
lngPH As Long
lngDFI As Long
lngDFr As Long
End Type
Public Sub PaperAndOrient(ByVal strName As String)
Const DM_PORTRAIT = 1
Const DM_LANDSCAPE = 2
Const DM_PAPERSIZE = 9
Dim DevString As str_DEVMODE
Dim DM As type_DEVMODE
Dim strDevModeExtra As String
Dim rpt As Report
' Opens report in Design view.
DoCmd.OpenReport strName, acDesign
Set rpt = Reports(strName)
If Not IsNull(rpt.PrtDevMode) Then
strDevModeExtra = rpt.PrtDevMode
DevString.RGB = strDevModeExtra
LSet DM = DevString
DM.lngFields = DM.lngFields Or DM.intOrientation
'Initialize fields.
DM.intPaperSize = DM_PAPERSIZE
If DM.intOrientation = DM_PORTRAIT Then
DM.intOrientation = DM_LANDSCAPE
End If
' Update property.
LSet DevString = DM
Mid(strDevModeExtra, 1, 94) = DevString.RGB
rpt.PrtDevMode = strDevModeExtra
End If
DoCmd.Close acReport, strName, acSaveYes
DoCmd.OpenReport strName, acViewPreview
Set rpt = Nothing
End SubThe User-Defined Types str_DEVMODE and Type_DEVMODE
At the beginning of the code, two new user-defined data types, str_DEVMODE and type_DEVMODE, are declared. The report’s PrtDevMode property value is copied into this structured data area, allowing us to modify specific elements and update them back into the report before printing.
Within str_DEVMODE, a member named RGB is defined as a 94-byte string. This 94-byte area contains 26 different parameters of various data types and sizes, which are individually defined under the type_DEVMODE structure. By transferring the data from str_DEVMODE (a single block of 94 characters) into type_DEVMODE, we can modify individual parameter values before writing them back into the report’s Page Setup.
NB: If the Database is implemented with Microsoft Access Security, then all Users must have the Report Design Change Authority to run this procedure.
Preparing for a Trial Run
To try out our Program, open one of your Reports with Landscape Page Orientation in Design View.
Select Page Setup from the File Menu.
Select the Page Tab on the Dialog Box.
Change Orientation to Portrait.
Change Paper Size to A6.
Save the Report and open it in Print Preview to check how it looks with the change.
Close the Report after viewing.
Create a Command Button on an existing Form or on a new Form and keep the Form in Design View.
Display the Property Sheet of the Command Button (Alt+Enter or View -> Properties).
Change the Name Property Value to cmdPreview.
Copy and paste the following code into the Code Module (View -> Code) of the Form.
Private Sub cmdPreview_Click() PaperAndOrient "MyReport" End SubReplace "MyReport" with your own Report Name.
Save the Form and open it in a normal view.
Click the cmdPreview button to run the program, to change the Page setup correctly, and open it in Print Preview.
Type PaperAndOrient "MyReport" in the Debug Window (Ctrl+G) and press Enter to run the Program directly without the Form and Command Button.
Open the Report again in design view and check whether the erraneous changes that you have made manually in the Page Setup Dialog Box, to test the program, have now been corrected through the program or not.
Next, we will see how to change the values on the Margins tab of the Page Setup Dialog Box through the Program.











No comments:
Post a Comment
Comments subject to moderation before publishing.