Continued from Last Week's Page.
In an earlier article, we discussed how to automatically change the paper size and page orientation of MS Access reports through VBA code for any printer on the network. We achieved this by copying the PrtDevMode property values into memory, modifying them to match the desired paper size and page orientation, and then updating them back into the report’s page settings before printing it on the user’s default printer.
Working with the PrtMip Property
We will have a similar exercise to change the Margin Settings of the MS-Access Report through the program. This time, we need to work with the PrtMip Property of the Report to change the Margin Values.
The procedure is similar to the previous example. The steps taken in the Program are as follows:
Open the Report in Design View.
Copy the PrtMip Property Values of the Report into a 28-byte-long, String Variable and move it into a redefined structured data area for modification.
Change the required Margin Values in Memory.
Update them back into the Report's PrtMip Property.
Save the Report with the changes and open it in Print Preview.
Prepare for a Demo Run
So let us start.
Open one of your Databases with Reports in it.
Display the Visual Basic Editing Window (Alt+Fll).
Insert (Insert -> Module) a new Standard Module (Global Module).
Copy and paste the following code into the new Module and save it.
Private Type str_PRTMIP strRGB As String * 28 End Type Private Type type_PRTMIP xLeftMargin As Long yTopMargin As Long xRightMargin As Long yBotMargin As Long fDataOnly As Long xWidth As Long yHeight As Long fDefaultSize As Long cxColumns As Long yColumnSpacing As Long xRowSpacing As Long rItemLayout As Long fFastPrint As Long fDatasheet As Long End Type Public Sub SetMargins(ByVal strName As String) Dim PrtMipString As str_PRTMIP Dim PM As type_PRTMIP Dim rpt As Report Const TWIPS As Long = 1440 ' Open the report. DoCmd.OpenReport strName, acDesign Set rpt = Reports(strName) PrtMipString.strRGB = rpt.PrtMip LSet PM = PrtMipString ' Set margins. PM.xLeftMargin = 0.75 * TWIPS PM.yTopMargin = 0.5 * TWIPS PM.xRightMargin = 0.5 * TWIPS PM.yBotMargin = 0.5 * TWIPS ' Update property. LSet PrtMipString = PM rpt.PrtMip = PrtMipString.strRGB DoCmd.Close acReport, strName, acSaveYes DoCmd.OpenReport strName, acViewPreview Set rpt = Nothing End Sub
Open one of your existing Reports in Design View.
Select File -> Page Setup -> Margins.
Change all four sides (Left, Right, Top, and Bottom) of the Margin settings to 1 Inch.
Save and Close the Report.
Open the Main Switchboard Form of your Application or create a new Form.
Create a new Command Button on the Form.
While the Command Button is in the selected state, display the Property Sheet (View ->Properties).
Change the Name Property Value to cmdPreview and change the Caption Property Value to Print Preview.
Display the Code Module of the Form (View -> Code).
Copy and paste the following lines into the Code Module of the Form.
Private Sub cmdPreview_Click() SetMargins "MyReport" End Sub
Replace the name MyReport with your own Report Name.
Save and close the Form.
Open the Form in normal view and click on the Command Button to run the Program and change all four margins of the Report to new values, and open the Report in Print Preview.
Close the Report and open it again in Design View and check whether the margin settings have really changed through the program or not.
Note: If any value falls below the allowable range defined by the printer driver settings, the printer may automatically adjust it to the minimum acceptable value. As a result, you might notice that some settings appear higher than the values you originally specified.
Running SetMargins() Program from PaperandOrient() Sub-Routine
You can do a sample run of the Program by typing SetMargins "YourReportName" in the Debug Window directly, without the use of a Form or a Command Button.
You can run this program from within the earlier PaperAndOrient() Program to change the Margins also along with the Paper Size and Page Orientation. All three sets of values can be changed by calling the PaperAndOrient() Program alone.
The modified PaperAndOrient() Program is given below:
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 SetMargins strName DoCmd.Close acReport, strName, acSaveYes DoCmd.OpenReport strName, acViewPreview Set rpt = Nothing End Sub
Public Sub SetMargins(ByVal strName As String) Dim PrtMipString As str_PRTMIP Dim PM As type_PRTMIP Dim rprt As Report Const TWIPS As Long = 1440 Set rprt = Reports(strName) PrtMipString.strRGB = rprt.PrtMip LSet PM = PrtMipString ' Set margins. PM.xLeftMargin = 0.75 * TWIPS PM.yTopMargin = 0.5 * TWIPS PM.xRightMargin = 0.5 * TWIPS PM.yBotMargin = 0.5 * TWIPS ' Update property. LSet PrtMipString = PM rprt.PrtMip = PrtMipString.strRGB Set rprt = Nothing End Sub
The Measurement Unit is Twips.
- 1 Inch = 1440 Twips
- 1 Inch = 72 Points
- 1 Point = 20 Twips OR 1 Twip = 1/20 Point
The measurements of reports and their objects are handled internally in twips, rather than in inches or millimeters. Although you can specify measurements in standard units such as inches, centimeters, or other regional formats on the property sheets of reports, forms, or controls, MS Access automatically converts these values into twips internally. However, when working in VBA, this conversion must be done manually before changing the property values of objects.
For simplicity, we have used constant values in the program for page size, orientation, and margins. However, you can modify the code to pass these values as parameters—along with the report name—when calling the program for each report. This approach offers greater flexibility, allowing the same program to handle reports with different page settings.
Next, we’ll explore how to modify the values on the Columns tab of the Page Setup dialog box, available from the File menu.
No comments:
Post a Comment
Comments subject to moderation before publishing.