Solved

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

Posted on 2004-04-29
10
585 Views
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!
0
Comment
Question by:pmcontractor
10 Comments
 
LVL 34

Expert Comment

by:flavo
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!

Dave
0
 
LVL 34

Accepted Solution

by:
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
0
 
LVL 11

Expert Comment

by:Mighty_Silly
ID: 10955424
Greetings!

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

-Silly-
0
 
LVL 1

Assisted Solution

by:moralju
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_SHOWMINIMIZED = 2
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
 
 
 
 
Type WM_POINTAPI
        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
 
Type WM_WINDOWPLACEMENT
        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)
Wend
 
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()
Else
    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"
      Else
        loX = WM_apiShowWindow(hWndAccessApp, nCmdShow)
        Err.Clear
      End If
    Else
        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"
        Else
            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()
DoCmd.Restore

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************



0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 1

Expert Comment

by:moralju
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)
0
 
LVL 34

Expert Comment

by:flavo
ID: 10955941
or you could do it the easy way.. re my first post
0
 
LVL 1

Expert Comment

by:moralju
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..
0
 
LVL 8

Expert Comment

by:Benjamin_Luk
ID: 10956648
Just using Movesize action is enough

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

Setting

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


Ben
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

In the previous article, Using a Critera Form to Filter Records (http://www.experts-exchange.com/A_6069.html), the form was basically a data container storing user input, which queries and other database objects could read. The form had to remain op…
Regardless of which version on MS Access you are using, one of the harder data-entry forms to create is one where most data from previous entries needs to be appended to new records, especially when there are numerous fields and records involved.  W…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

747 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now