Learn Microsoft Access Advanced Programming Techniques, Tips and Tricks.

Network And Print Page Setup-2

Continued from Last Week's Page

We have seen in the earlier Article how to change Paper Size and Page Orientation of MS-Access Reports automatically through program for any Printer on the Network. We have copied PrtDevMode Property Values into Memory; modified them to match the Paper Size and Page Orientation of the Report and updated them back into the Report Page Settings before printing it on the default Printer installed on the User's Machine.


Working with the PrtMip Property

We are going to perform a similar exercise to change the Margin Settings of MS-Access Report through program. This time we have to work with PrtMip Property of the Report to change the Margin Values.

The procedure is almost same as of the previous example. The steps taken in the Program is as follows:

  • Open the Report in Design View.
  • Copy the PrtMip Property Values of the Report into a 28 Bytes 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.

  1. Open one of your Databases with Reports in it.
  2. Display the Visual Basic Editing Window (Alt+Fll).
  3. Insert (Insert - - > Module) a new Standard Module (Global Module).
  4. 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
  5. Open one of your existing Reports in Design View.
  6. Select File - ->Page Setup - - > Margins.
  7. Change all four sides (Left, Right, Top and Bottom) of the Margin settings to 1 Inch.
  8. Save and Close the Report.
  9. Open the Main Switchboard Form of your Application or create a new Form.
  10. Create a new Command Button on the Form.
  11. While the Command Button is in selected state, display the Property Sheet (View - - >Properties).
  12. Change the Name Property Value to cmdPreview and change the Caption Property Value to Print Preview.
  13. Display the Code Module of the Form (View - -> Code).
  14. Copy and paste the following lines into the Code Module of the Form. 
    Private Sub cmdPreview_Click()
      SetMargins "MyReport"
     End Sub
  15. Replace the name MyReport with your own Report Name.
  16. Save and close the Form.
  17. 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 to open the Report in Print Preview.

Close the Report and open it again in Design View and check whether the margin settings have been really changed through program or not.

Note:If any value is less than the allowable range, based on the printer driver settings, the printer may fix it to the allowable range automatically. In that case you may find some value is greater than what you have 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 Form or 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 the three set 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

The Dimensions of the Reports, Forms and other Objects are measured internally in Twips rather than in Inches or Millimeters. We are allowed to change the measurements on the Property Sheets of Report, Form or other Objects in standard measurements manually, like Inches or Centimeters or in any other regional values. They are automatically converted into Twips internally. But, in programs we have to do that work before changing the values of the Property of objects.

  • 1 Inch = 1440 Twips
  • 1 Inch = 72 Points
  • 1 Point = 20 Twips OR 1 Twip = 1/20 Point

We have used Constant values in the Program for Page Size, Orientation and Margins for simplicity. You can modify the Code to pass these values as parameters along with the Report Name, when the Program is called for each Report. This will give more flexibility and the Program can be called for Reports with different Page Settings.

Next we will see how to change the values on the Columns Tab of the Page Setup Dialog Box of File Menu.

Share:

No comments:

Post a Comment

Comments are subject to moderation before publishing.

Translate



PageRank
Subscribe in a reader
Your email address:

Delivered by FeedBurner

http://clicky.com/101199826
Blog Directory Visit blogadda.com to discover Indian blogs

Search

Popular Posts

Search This Blog

Blog Archive

Powered by Blogger.

Follow by Email

Labels

Forms How Tos Functions MS-Access Security Reports msaccess forms Animations Class Module msaccess animation Utilities msaccess controls Access and Internet MS-Access Scurity MS-Access and Internet Array Queries msaccess reports External Links msaccess tips Accesstips Downloads Objects Property Event Menus and Toolbars Collection Object Controls MsaccessLinks Process Controls WithEvents Art Work msaccess How Tos Combo Boxes Graph Charts List Boxes VBA msaccessQuery Command Buttons Form Command Button Data Dictionary Object Emails and Alerts Query RaiseEvent Report Calculation Custom Wizards DOS Commands Data Type ms-access functions msaccess functions msaccess graphs msaccess reporttricks Custom Functions Item Object Reference msaccessprocess security advanced Access Security Add Field Type Fields Form Instances Key Macros Menus SubForm Top Values Variables msaccess email msaccess menus progressmeter Access2007 Auto-Number Copy Excel Expression Join Methods Microsoft Numbering System Records Recordset Security Split Table Time Difference Utility Workgroup Wrapper Classes database function ms-access msaccess wizards reference text tutorial vba code Access2003 Accounting Year Action Animation Attachment Binary Numbers Bookmarks Budgeting ChDir Color Palette Conditional Formatting Data Filtering Defining Pages Diagram Disk Dynamic Lookup Error Handler Export External Filter Formatting Groups Hexadecimal Numbers Import Labels List Logo Macro Mail Merge Main Form Memo Monitoring Octal Numbers Operating System Paste Primary-Key Product Rank Reading Remove Rich Text Sequence SetFocus Summary Tab-Page Tables Union Query User Users Water-Mark Word automatically commands hyperlinks iSeries Date iif msaccess msaccess alerts pdf files restore switch toolbar updating upload

Featured Post

Call Function From MouseMove Event Property

Introduction. This is about running a User-Defined Function (say myFunction(Parameter) ) on the Mouse Move Event Property of Text Boxes.  W...

Labels

Blog Archive

Recent Posts