When I Open a form, I want to position it at a certain place on the screen.

Posted on 2004-04-29
Last Modified: 2012-05-04
When I Open a form during the execution of an application, how do I:

1. Using VBA, how do I place it in a certain position each time?
2. At the same time I would like to hide the form that called for it!
Question by:pmcontractor
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 34

Expert Comment

ID: 10955195
Go in to design mode, put the form where you want it, press save (make sure auto centre is turned off in the properties) and bob's your uncle!

Good Luck!

LVL 34

Accepted Solution

flavo earned 125 total points
ID: 10955204
>> 2. At the same time I would like to hide the form that called for it!

Me.Visible = False
Docmd.OpenForm "myOtherForm", acNormal
LVL 11

Expert Comment

ID: 10955424

I normally add Docmd.Restore to the form's Form Open event also.

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Assisted Solution

moralju earned 125 total points
ID: 10955910

<Copy and paste this in a module>

'**********Code Start********
Option Compare Database
Option Explicit

Global Const WM_SM_CXVSCROLL = 2
Global Const WM_SM_CYCAPTION = 4
Global Const WM_SM_CXBORDER = 5
Global Const WM_SM_CYBORDER = 6
Global Const WM_SM_CXDLGFRAME = 7
Global Const WM_SM_CYDLGFRAME = 8
Global Const WM_SM_CYMENU = 15
Global Const WM_SM_CXFRAME = 32
Global Const WM_SM_CYFRAME = 33
Global Const WM_LOGPIXELSX = 88
Global Const WM_LOGPIXELSY = 90
Global Const WM_SWP_DRAWFRAME = &H20
Global Const WM_SWP_HIDEWINDOW = &H80
Global Const WM_SWP_NOACTIVATE = &H10
Global Const WM_SWP_NOCOPYBITS = &H100
Global Const WM_SWP_NOMOVE = &H2
Global Const WM_SWP_NOREDRAW = &H8
Global Const WM_SWP_NOREPOSITION = &H200
Global Const WM_SWP_NOSIZE = &H1
Global Const WM_SWP_NOZORDER = &H4
Global Const WM_SWP_SHOWWINDOW = &H40
Global Const WM_SW_MAXIMIZE = 3
Global Const WM_SW_MINIMIZE = 6
Global Const WM_SW_NORMAL = 1
Global Const WM_SW_SHOWNORMAL = 1
Global Const WM_SW_RESTORE = 9
Global Const WM_SW_HIDE = 0
Global Const WM_SW_SHOW = 5
Global Const WM_HORZRES = 8
Global Const WM_VERTRES = 10
Dim i As Integer
Global iJustOpened As Integer
Dim iFormPosition As Integer
Global iFormPositioningMode As Integer
Global iFormTop As Integer
Global iFormLeft As Integer
        X As Long
        Y As Long
End Type
Type WM_Rect
        Left As Long
        Top As Long
        Right As Long
        Bottom As Long
End Type
        Length As Long
        flags As Long
        showCmd As Long
        ptMinPosition As WM_POINTAPI
        ptMaxPosition As WM_POINTAPI
        rcNormalPosition As WM_Rect
End Type
Declare Function WM_apiSetWindowPos Lib "user32" Alias "SetWindowPos" (ByVal hWnd As Long, ByVal hWndInsertAfter As Long, ByVal X As Long, ByVal Y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
Declare Function WM_apiGetParent Lib "user32" Alias "GetParent" (ByVal hWnd As Long) As Long
Declare Function WM_apiShowWindow Lib "user32" Alias "ShowWindow" (ByVal hWnd As Long, ByVal nCmdShow As Long) As Long
Declare Function WM_apiGetDesktopWindow Lib "user32" Alias "GetDesktopWindow" () As Long
Declare Function WM_apiGetDC Lib "user32" Alias "GetDC" (ByVal hWnd As Long) As Long
Declare Function WM_apiReleaseDC Lib "user32" Alias "ReleaseDC" (ByVal hWnd As Long, ByVal hdc As Long) As Long
Declare Function WM_apiGetDeviceCaps Lib "gdi32" Alias "GetDeviceCaps" (ByVal hdc As Long, ByVal nIndex As Long) As Long
Declare Function WM_apiGetSystemMetrics Lib "user32" Alias "GetSystemMetrics" (ByVal nIndex As Long) As Long
Declare Function WM_apiGetWindowPlacement Lib "user32" Alias "GetWindowPlacement" (ByVal hWnd As Long, lpwndpl As WM_WINDOWPLACEMENT) As Long
Declare Function WM_apiIsZoomed Lib "user32" Alias "IsZoomed" (ByVal hWnd As Long) As Long
Declare Function WM_apiGetClientRect Lib "user32" Alias "GetClientRect" (ByVal hWnd As Long, lpRect As WM_Rect) As Long
Function xg_GetAccesshWnd() As Long
'Returns a handle to the main Access window
Dim hWnd As Long
Dim hAccessWnd As Long
hWnd = Screen.ActiveForm.hWnd
hAccessWnd = hWnd
'* Find the top window without a parent window.
While hWnd <> 0
    hAccessWnd = hWnd
    hWnd = WM_apiGetParent(hWnd)
xg_GetAccesshWnd = hAccessWnd
End Function

Sub xg_SizeWindow(sWindow As String, X As Integer, Y As Integer, cx As Integer, cy As Integer)
' Size the active form window, or the main Access window.
' Parameter sWindow:  "Active" - Size the active form window.
'                     "Access" - Size the main Access window.
'                 x:  left boundry for window (pixels)
'                 y:  top boundry for window
'                cx:  width of window
'                cy:  height of window
Dim iRtn As Integer
Dim hWndSize As Long
If sWindow = "Active" Then
    hWndSize = Screen.ActiveForm.hWnd
ElseIf sWindow = "Access" Then
    hWndSize = xg_GetAccesshWnd()
    MsgBox "Invalid parameter passed to xg_SizeWindow = " & sWindow
    Exit Sub
End If
iRtn = WM_apiShowWindow(hWndSize, WM_SW_RESTORE)
Call WM_apiSetWindowPos(hWndSize, 0, X, Y, cx, cy, WM_SWP_NOZORDER Or WM_SWP_SHOWWINDOW)
End Sub

Function fSetAccessWindow(nCmdShow As Long)
'Usage Examples
'Maximize window:
'       ?fSetAccessWindow(SW_SHOWMAXIMIZED)
'Minimize window:
'       ?fSetAccessWindow(SW_SHOWMINIMIZED)
'Hide window:
'       ?fSetAccessWindow(SW_HIDE)
'Normal window:
'       ?fSetAccessWindow(SW_SHOWNORMAL)
Dim loX  As Long
Dim loForm As Form
    On Error Resume Next
    Set loForm = Screen.ActiveForm
    If Err <> 0 Then 'no Activeform
      If nCmdShow = WM_SW_HIDE Then
        MsgBox "Cannot hide Access unless " _
                    & "a form is on screen"
        loX = WM_apiShowWindow(hWndAccessApp, nCmdShow)
      End If
        If nCmdShow = WM_SW_SHOWMINIMIZED And loForm.Modal = True Then
            MsgBox "Cannot minimize Access with " _
                    & (loForm.Caption + " ") _
                    & "form on screen"
        ElseIf nCmdShow = WM_SW_HIDE And loForm.PopUp <> True Then
            MsgBox "Cannot hide Access with " _
                    & (loForm.Caption + " ") _
                    & "form on screen"
            loX = WM_apiShowWindow(hWndAccessApp, nCmdShow)
        End If
    End If
    fSetAccessWindow = (loX <> 0)
End Function

'************Code End***********

<Place this code on your Form load>

'**********Code start*******
Private Sub Form_Load()

Dim X As Integer, Y As Integer, cx As Integer, cy As Integer

'resize window
X = 25 ' this position the windows at your wish
Y = 25

cx = 588 ' make the window what size you wish
cy = 950
xg_SizeWindow "active", X, Y, cx, cy
End Sub

'**********Code end************


Expert Comment

ID: 10955928
YOu can also place this in your Form open property and make the Access Container Maximize

'Maximize Access window
fSetAccessWindow (WM_SW_MAXIMIZE)
LVL 34

Expert Comment

ID: 10955941
or you could do it the easy way.. re my first post

Expert Comment

ID: 10955979
Flavo, I really think its up to pmcontractor to decide.
debating over who has a better option is a waste of space and time.

As per EE culture..

Expert Comment

ID: 10956648
Just using Movesize action is enough

You can use the MoveSize action to move or resize the active window.


The MoveSize action has the following arguments.

Action argument      Description
Right      The new horizontal position of the window's upper-left corner, measured from the left edge of its containing window. Enter the position in the Right box in the Action Arguments section of the Macro window.
Down      The new vertical position of the window's upper-left corner, measured from the top edge of its containing window.
Width      The window's new width.
Height      The window's new height.

Best Regards


Featured Post

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question