Open a form full screen

gissah

Registered User.
Local time
Yesterday, 19:25
Joined
Dec 9, 2008
Messages
17
I am using macro to open a form when I open my DBA. But I want it to open the full form instead of just a small form in the middle of my desk top
 
In your macro, after the OpenForm command put a Maximize command. You may want to run a Restore command when you close the form if you don't want other forms Maximized.
 
I borrowed from some other people's code and developed what I call a 'Soft Maximize' option. I didn't want to do a normal Maximize, because then every form is maximized. I simply wanted some of my forms to use ALL of the available screen.

If this is what you want, put the following code in a Class Module named "basFormWindow"

Code:
Option Compare Database
Option Explicit
'*************************************************************
' Class module: basFormWindow                                 *
'*************************************************************
' Moves and resizes a window in the coordinate system        *
' of its parent window.                                      *
' N.B.: This class was developed for use on Access forms     *
'       and has not been tested for use with other window    *
'       types.                                               *
'*************************************************************
'*************************************************************
' Type declarations
'*************************************************************
Private Type RECT       'RECT structure used for API calls.
    Left As Long
    Top As Long
    Right As Long
    Bottom As Long
End Type
 
Private Type POINTAPI   'POINTAPI structure used for API calls.
    X As Long
    Y As Long
End Type
'*************************************************************
' Member variables
'*************************************************************
Private m_hWnd As Long          'Handle of the window.
Private m_rctWindow As RECT     'Rectangle describing the sides of the last polled location of the window.
 
'*************************************************************
' Private error constants for use with RaiseError procedure
'*************************************************************
Private Const m_ERR_INVALIDHWND = 1
Private Const m_ERR_NOPARENTWINDOW = 2
 
'*************************************************************
' API function declarations
'*************************************************************
Private Declare Function apiIsWindow Lib "user32" Alias "IsWindow" (ByVal hWnd As Long) As Long
Private Declare Function apiMoveWindow Lib "user32" Alias "MoveWindow" (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
    'Moves and resizes a window in the coordinate system of its parent window.
Private Declare Function apiGetWindowRect Lib "user32" Alias "GetWindowRect" (ByVal hWnd As Long, lpRect As RECT) As Long
    'After calling, the lpRect parameter contains the RECT structure describing the sides of the window in screen coordinates.
Private Declare Function apiScreenToClient Lib "user32" Alias "ScreenToClient" (ByVal hWnd As Long, lpPoint As POINTAPI) As Long
    'Converts lpPoint from screen coordinates to the coordinate system of the specified client window.
Private Declare Function apiGetParent Lib "user32" Alias "GetParent" (ByVal hWnd As Long) As Long
    'Returns the handle of the parent window of the specified window.
 
'*************************************************************
' Private procedures
'*************************************************************
Private Sub RaiseError(ByVal lngErrNumber As Long, ByVal strErrDesc As String)
'Raises a user-defined error to the calling procedure.
    Err.Raise vbObjectError + lngErrNumber, "basFormWindow", strErrDesc
 
End Sub
 
Private Sub UpdateWindowRect()
'Places the current window rectangle position (in pixels, in coordinate system of parent window) in m_rctWindow.
    Dim ptCorner As POINTAPI
 
    If m_hWnd = 0 Or apiIsWindow(m_hWnd) Then
        apiGetWindowRect m_hWnd, m_rctWindow   'm_rctWindow now holds window coordinates in screen coordinates.
 
        If Not Me.Parent Is Nothing Then
            'If there is a parent window, convert top, left of window from screen coordinates to parent window coordinates.
            With ptCorner
                .X = m_rctWindow.Left
                .Y = m_rctWindow.Top
            End With
 
            apiScreenToClient Me.Parent.hWnd, ptCorner
 
            With m_rctWindow
                .Left = ptCorner.X
                .Top = ptCorner.Y
            End With
 
            'If there is a parent window, convert bottom, right of window from screen coordinates to parent window coordinates.
            With ptCorner
                .X = m_rctWindow.Right
                .Y = m_rctWindow.Bottom
            End With
 
            apiScreenToClient Me.Parent.hWnd, ptCorner
 
            With m_rctWindow
                .Right = ptCorner.X
                .Bottom = ptCorner.Y
            End With
        End If
    Else
        RaiseError m_ERR_INVALIDHWND, "The window handle " & m_hWnd & " is no longer valid."
    End If
 
End Sub
 
 
'*************************************************************
' Public read-write properties
'*************************************************************
Public Property Get hWnd() As Long
'Returns the value the user has specified for the window's handle.
    If m_hWnd = 0 Or apiIsWindow(m_hWnd) Then
        hWnd = m_hWnd
    Else
        RaiseError m_ERR_INVALIDHWND, "The window handle " & m_hWnd & " is no longer valid."
    End If
 
End Property
 
Public Property Let hWnd(ByVal lngNewValue As Long)
'Sets the window to use by specifying its handle.
'Only accepts valid window handles.
    If lngNewValue = 0 Or apiIsWindow(lngNewValue) Then
        m_hWnd = lngNewValue
    Else
        RaiseError m_ERR_INVALIDHWND, "The value passed to the hWnd property is not a valid window handle."
    End If
 
End Property
'----------------------------------------------------
Public Property Get Left() As Long
'Returns the current position (in pixels) of the left edge of the window in the coordinate system of its parent window.
    If m_hWnd = 0 Or apiIsWindow(m_hWnd) Then
        UpdateWindowRect
        Left = m_rctWindow.Left
    Else
        RaiseError m_ERR_INVALIDHWND, "The window handle " & m_hWnd & " is no longer valid."
    End If
 
End Property
 
Public Property Let Left(ByVal lngNewValue As Long)
'Moves the window such that its left edge falls at the position indicated
'(measured in pixels, in the coordinate system of its parent window).
    If m_hWnd = 0 Or apiIsWindow(m_hWnd) Then
        UpdateWindowRect
        With m_rctWindow
            apiMoveWindow m_hWnd, lngNewValue, .Top, .Right - .Left, .Bottom - .Top, True
        End With
    Else
        RaiseError m_ERR_INVALIDHWND, "The window handle " & m_hWnd & " is no longer valid."
    End If
 
End Property
Public Property Get Top() As Long
'Returns the current position (in pixels) of the top edge of the window in the coordinate system of its parent window.
    If m_hWnd = 0 Or apiIsWindow(m_hWnd) Then
        UpdateWindowRect
        Top = m_rctWindow.Top
    Else
        RaiseError m_ERR_INVALIDHWND, "The window handle " & m_hWnd & " is no longer valid."
    End If
End Property
Public Property Let Top(ByVal lngNewValue As Long)
'Moves the window such that its top edge falls at the position indicated
'(measured in pixels, in the coordinate system of its parent window).
    If m_hWnd = 0 Or apiIsWindow(m_hWnd) Then
        UpdateWindowRect
        With m_rctWindow
            apiMoveWindow m_hWnd, .Left, lngNewValue, .Right - .Left, .Bottom - .Top, True
        End With
    Else
        RaiseError m_ERR_INVALIDHWND, "The window handle " & m_hWnd & " is no longer valid."
    End If
End Property
'----------------------------------------------------
Public Property Get Width() As Long
'Returns the current width (in pixels) of the window.
 
    If m_hWnd = 0 Or apiIsWindow(m_hWnd) Then
        UpdateWindowRect
        With m_rctWindow
            Width = .Right - .Left
        End With
    Else
        RaiseError m_ERR_INVALIDHWND, "The window handle " & m_hWnd & " is no longer valid."
    End If
End Property
 
Public Property Let Width(ByVal lngNewValue As Long)
'Changes the width of the window to the value provided (in pixels).
    If m_hWnd = 0 Or apiIsWindow(m_hWnd) Then
        UpdateWindowRect
        With m_rctWindow
            apiMoveWindow m_hWnd, .Left, .Top, lngNewValue, .Bottom - .Top, True
        End With
    Else
        RaiseError m_ERR_INVALIDHWND, "The window handle " & m_hWnd & " is no longer valid."
    End If
End Property
'----------------------------------------------------
Public Property Get Height() As Long
'Returns the current height (in pixels) of the window.
 
    If m_hWnd = 0 Or apiIsWindow(m_hWnd) Then
        UpdateWindowRect
        With m_rctWindow
            Height = .Bottom - .Top
        End With
    Else
        RaiseError m_ERR_INVALIDHWND, "The window handle " & m_hWnd & " is no longer valid."
    End If
End Property
Public Property Let Height(ByVal lngNewValue As Long)
'Changes the height of the window to the value provided (in pixels).
    If m_hWnd = 0 Or apiIsWindow(m_hWnd) Then
        UpdateWindowRect
        With m_rctWindow
            apiMoveWindow m_hWnd, .Left, .Top, .Right - .Left, lngNewValue, True
        End With
    Else
        RaiseError m_ERR_INVALIDHWND, "The window handle " & m_hWnd & " is no longer valid."
    End If
End Property
 
'*************************************************************
' Public read-only properties
'*************************************************************
Public Property Get Parent() As basFormWindow
'Returns the parent window as a basFormWindow object.
'For forms, this should be the Access MDI window.
    Dim fwParent As New basFormWindow
    Dim lngHWnd As Long
 
    If m_hWnd = 0 Then
        Set Parent = Nothing
    ElseIf apiIsWindow(m_hWnd) Then
        lngHWnd = apiGetParent(m_hWnd)
        fwParent.hWnd = lngHWnd
        Set Parent = fwParent
    Else
        RaiseError m_ERR_INVALIDHWND, "The window handle " & m_hWnd & " is no longer valid."
    End If
    Set fwParent = Nothing
 
End Property

Add this code to a Standard Module called 'ModFormWindow':

Code:
Option Compare Database
Option Explicit
Option Base 1
Private Declare Function apiGetSys Lib "user32" Alias "GetSystemMetrics" (ByVal nIndex As Long) As Long
Private Const SM_CXSCREEN = 0
Private Const SM_CYSCREEN = 1
Function fGetScreenSize()
    On Error GoTo ErrLog
    
    Dim strRes As String
    strRes = apiGetSys(SM_CXSCREEN) & "x" & apiGetSys(SM_CYSCREEN)
    Exit Function
ErrLog:
    Call ErrorLog(Err.Number, Err.Description, "modFormWindow.fGetScreenSize")
    Resume Next
End Function
Sub SoftMaximize(ByVal strFormName As String)
    On Error GoTo ErrLog
  
    Dim fwForm As New basFormWindow
    Const SMALL_OFFSET = 4  'Used to avoid appearance of the scroll bars
    With fwForm
        .hWnd = Forms(strFormName).hWnd
        .Left = 0
        .Top = 0
        .Width = .Parent.Width - SMALL_OFFSET
        .Height = .Parent.Height - SMALL_OFFSET
    End With
    Set fwForm = Nothing
    Exit Sub
ErrLog:
    Call ErrorLog(Err.Number, Err.Description, "modFormWindow.SoftMaximize")
    Resume Next
End Sub

Then you can call the SoftMaximize procedure from any form:

Code:
SoftMaximize Me.Name

The only issue I have seen with this is that forms with custom toolbars don't always get sized properly. Other that this, it works very well.

Hope this helps,
Evan
 
I just added this code to the form code:

Code:
Private Sub Form_Load()
    DoCmd.Maximize
End Sub
and then make sure 'Pop Up' and 'Modal' are selected as 'Yes' in the form properties - to make it open when you open the database!

Works a treat!
 

Users who are viewing this thread

Back
Top Bottom