Home  |   French  |   About  |   Search  | mvps.org  

What's New
Table Of Contents
Credits
Netiquette
10 Commandments 
Bugs
Tables
Queries
Forms
Reports
Modules
APIs
Strings
Date/Time
General
Downloads
Resources
Search
Feedback
mvps.org

In Memoriam

Terms of Use


VB Petition

API: Removing Min/Max/Close buttons from a report's Preview Window

Author(s)
Terry Kreft &
Keri Hardwick

To remove the Minimize, Maximize, and Restore button from a Report's preview window, we have to create a custom toolbar to allow the user to close the report and  use some API functions to remove the caption bar from the preview window.  This way, once you maximize the Report, the preview pane will occupy the entire Access screen without automatically enabling the restore and other buttons.

For reference, here are the steps I've used:

  1. In every report, in the Deactivate event:
  2. DoCmd.Close acReport, Me.Name
  3. I have a main menu form (frmStart) that is always open, and is the only "legal" place to exit the db. In that form's declarations:
  4. Dim CanClose as Integer

    In Open:

    CanClose = 0

    In Unload:

    If Not CanClose Then
        Cancel = True
        Forms!frmStart.Visible = True
        'form is hidden when report is previewed
        DoCmd.SelectObject acForm, "frmStart"
        CloseForms
        CloseReports
        ' CloseForms and CloseReports close any open objects
        ' other than frmStart
      End If
  5. Take DoCmd.Maximize out of the Open event of each form.
  6. Create a custom toolbar for reports with Print, Close and any other desired options. Put toolbar name in reports' toolbar property. Set report's menu to "-1". Doing these two things limits the user's available actions to the items on your toolbar.
  7. Use this set of code to open the reports in preview:
  8. DoCmd.OpenReport "TheReport", acViewPreview,...
    Call sRemoveCaption(Reports("TheReport")

What happens as a result of this effort: Your user can

  1. See the report
  2. Do the things your toolbar allows
  3. Minimize the Access app

That's all. If they try to close the Access app, they get returned to the main menu. There are no buttons to min, max or close the report window itself.

'********************* Code Start ************************
'This code was originally written by Terry Kreft & Keri Hardwick.
'It is not to be altered or distributed,
'except as part of an application.
'You are free to use it in any application,
'provided the copyright notice is left unchanged.
'
'Code Courtesy of
'Terry Kreft & Keri Hardwick
'
Private Type RECT  '  16  Bytes
    left As Long
    top As Long
    right As Long
    bottom As Long
End Type
 
Private Declare Function apiGetWindowLong Lib "User32" _
    Alias "GetWindowLongA" _
    (ByVal hwnd As Long, _
    ByVal nIndex As Long) _
    As Long
 
Private Declare Function apiSetWindowLong Lib "User32" _
    Alias "SetWindowLongA" _
    (ByVal hwnd As Long, _
    ByVal nIndex As Long, _
    ByVal dwNewLong As Long) _
    As Long
 
Private Declare Function apiGetWindowRect Lib "User32" _
    Alias "GetWindowRect" _
    (ByVal hwnd As Long, _
    lpRect As RECT) _
    As Long
 
Private Declare Function apiGetSystemMetrics Lib "User32" _
    Alias "GetSystemMetrics" _
    (ByVal nIndex&) _
    As Long
 
Private Declare Function apiReleaseDC Lib "User32" _
  Alias "ReleaseDC" _
  (ByVal hwnd As Long, _
  ByVal hDC As Long) _
  As Long
 
Private Declare Function apiGetDeviceCaps Lib "Gdi32" _
    Alias "GetDeviceCaps" _
    (ByVal hDC As Long, _
    ByVal nIndex As Long) _
    As Long
 
Private Declare Function apiGetDC Lib "User32" _
    Alias "GetDC" _
    (ByVal hwnd As Long) _
    As Long
 
Private Declare Function IsZoomed Lib "User32" _
    (ByVal hwnd As Long) As Long
 
Private Declare Function ShowWindow Lib "User32" _
    (ByVal hwnd As Long, _
    ByVal nCmdShow As Long) As Long
 
Private Declare Function MoveWindow Lib "User32" _
    (ByVal hwnd As Long, _
    ByVal x As Long, _
    ByVal Y As Long, _
    ByVal nWidth As Long, _
    ByVal nHeight As Long, _
    ByVal bRepaint As Long) As Long
 
Private Declare Function GetParent Lib "User32" _
    (ByVal hwnd As Long) As Long
 
'Use following instead of GetWindowRect
Private Declare Function GetClientRect Lib "User32" _
    (ByVal hwnd As Long, _
    lpRect As RECT) As Long
 
Public Const SW_MAXIMIZE = 3
Public Const SW_SHOWNORMAL = 1
Private Const GWL_EXSTYLE = -20
Private Const GWL_HINSTANCE = -6
Private Const GWL_HWNDPARENT = -8
Private Const GWL_ID = -12
Private Const GWL_STYLE = -16
Private Const GWL_USERDATA = -21
Private Const GWL_WNDPROC = -4
Private Const WS_CAPTION = &HC00000
Private Const WS_SYSMENU = &H80000
Private Const SM_CYCAPTION = 4  ' Height of caption or title
Private Const TWIPSPERINCH = 1440
'**  Window Style Constants
Private Const WS_DLGFRAME& = &H400000
Private Const WS_THICKFRAME& = &H40000

Sub aTest()
    DoCmd.OpenReport "Report1", acViewPreview
    Call sRemoveCaption(Reports!Report1)
End Sub
 
Private Sub MaximizeRestoredReport(R As Report)
'This code was originally written by Terry Kreft
'Full credits and acknowledgements to him.
'
Dim MDIRect As RECT
    ' If the Report is maximized, restore it.
    If IsZoomed(R.hwnd) <> 0 Then
        ShowWindow R.hwnd, SW_SHOWNORMAL
    End If
 
    ' Get the screen coordinates and window size of the
    ' MDIClient area.
    'This is the line which is different
    GetClientRect GetParent(R.hwnd), MDIRect
 
    ' Move the Report to the upper left corner of the MDIClient
    ' window (0,0) and size it to the same size as the
    ' MDIClient window.
 
    MoveWindow R.hwnd, 0, 0, MDIRect.right - MDIRect.left, _
        MDIRect.bottom - MDIRect.top, True
End Sub
 
Sub sRemoveCaption(rpt As Report)
 
Dim lngRet As Long, lngStyle As Long
Dim tRECT As RECT, lngX As Long
Dim lngCaptionWidth As Long
Dim lngLeft As Long
Dim lngTop As Long
Dim lngWidth As Long
Dim lngHeight As Long
 
    lngRet = apiGetWindowLong(rpt.hwnd, GWL_STYLE)
    lngStyle = (lngRet Xor WS_DLGFRAME Xor _
                    WS_THICKFRAME) And Not WS_CAPTION
    'Need the Xor above to keep window from being sizable
    lngRet = apiSetWindowLong(rpt.hwnd, GWL_STYLE, lngStyle)
    lngX = apiGetWindowRect(rpt.hwnd, tRECT)
 
    'have to resize the form now
    'how much was caption's screenspace
    lngCaptionWidth = apiGetSystemMetrics(SM_CYCAPTION)
 
    With tRECT
        lngLeft = .left
        lngTop = .top
        lngWidth = .right - .left
        lngHeight = .bottom - .top - lngCaptionWidth
        ConvertPIXELSToTWIPS lngLeft, lngTop
        ConvertPIXELSToTWIPS lngWidth, lngHeight
        DoCmd.SelectObject acReport, rpt.Name, False
        DoCmd.Restore
        DoCmd.MoveSize lngLeft, lngTop, lngWidth, lngHeight
    End With
 
    'now use Terry's code here
    Call MaximizeRestoredReport(rpt)
 End Sub
 
Sub ConvertPIXELSToTWIPS(x As Long, Y As Long)
 'From the KB
    Dim hDC As Long, hwnd As Long, RetVal As Long
    Dim XPIXELSPERINCH, YPIXELSPERINCH
    Const LOGPIXELSX = 88
    Const LOGPIXELSY = 90
 
    hDC = apiGetDC(0)
    XPIXELSPERINCH = apiGetDeviceCaps(hDC, LOGPIXELSX)
    YPIXELSPERINCH = apiGetDeviceCaps(hDC, LOGPIXELSY)
    RetVal = apiReleaseDC(0, hDC)
    x = (x / XPIXELSPERINCH) * TWIPSPERINCH
    Y = (Y / YPIXELSPERINCH) * TWIPSPERINCH
End Sub
'************************ Code End **************************

© 1998-2010, Dev Ashish & Arvin Meyer, All rights reserved. Optimized for Microsoft Internet Explorer