Results 1 to 4 of 4
  1. #1
    Join Date
    Jan 2001
    New York, NY USA
    Thanked 0 Times in 0 Posts

    Using VBA to postion a form on the screen

    I created a small pop form that mimics a calculator for use with several controls on an Access form. I want to open the calculator pop form at different locations on the screen, depending on which control needs the calculations. Is there any way to position the calculator form programatically using VBA or some other method?

  2. #2

    Re: Using VBA to postion a form on the screen

    Yes, possibly.

    You need to write a stand-alone procedure.

    It checks the ActiveControl to see where it is on the calling form, and where the calling form is in Access' application window.

    Then you have to develop some rules as to where to pop up that form.

    The first part is easy:

    Public Sub OpenMyCalculator()
    Dim af as form, ac as control
    set af = screen.activeform
    set ac = screen.activecontrol

    'Application's client area (I think):

    'active form's size is:
    'af.Top, af.Left, af.Height, af.Width
    'active control's locations:
    ', ac.left, ac.height, ac.width

    'rules to determine where to open the calculator go here

    DoCmd.OpenForm "frmMyCalculator",,,,,,"Coords=(" & top & "," & left & ")"

    end sub

    ...then your calculator form needs to have an Open event handler that tries to parse the OpenArgs argument, if it's there.

    If it is there, then use:

    DoCmd.MoveSize the Open event handler to move the form.

  3. #3
    4 Star Lounger
    Join Date
    Jan 2001
    Altnau, Thurgau, Switzerland
    Thanked 0 Times in 0 Posts

    Re: Using VBA to postion a form on the screen

    There are a couple of problems to be sorted out before you can do what you want. If a form is not maximised then I don't think you can get at the positioning on screen through Access. Also the control has a Top and a Left property, but these are the positioning relative to the 0, 0 point of the form - not a dynamic positioning (form is scrolled). You will need some Windows API calls. The following is part of a routine I used some years ago (for an Access 95 based solution). You may need some slight modifications, I hope it is clear what to do with this.

    Type RECT
    Left As Long
    Top As Long
    Right As Long
    Bottom As Long
    End Type

    x As Long
    Y As Long
    End Type

    'Length is the size of this structure - must be set before using the structure
    Length As Long
    flags As Long
    showCmd As Long
    ptMinPosition As POINTAPI
    ptMaxPosition As POINTAPI
    rcNormalPosition As RECT
    End Type

    Declare Function GetFocus Lib "user32" () As Long

    Declare Function GetWindowPlacement Lib "user32" (ByVal hwnd As Long, lpwndpl As WINDOWPLACEMENT) As Long

    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

    '************************************************* *****
    OnClick event ( or whatever you use to start the calculator)

    dim hWndFocusControl as long
    hWndFocusControl = GetFocus()

    const cszForm = "Calculator Form"
    'open calculator form now
    docmd.openform cszForm

    Dim wndPlacement As WINDOWPLACEMENT
    Dim ret As Long
    wndPlacement.Length = Len(wndPlacement)
    ret = GetWindowPlacement(hWndFocusControl, wndPlacement)

    If ret Then
    Dim Y As Long, r As Long, w As Long, H As Long
    'save the 'position' of the control
    Y = wndPlacement.rcNormalPosition.Top
    r = wndPlacement.rcNormalPosition.Right

    'Current position of calculator form
    ret = GetWindowPlacement(Forms(cszForm).hwnd, wndPlacement)

    'fill structure with new position
    w = wndPlacement.rcNormalPosition.Right - wndPlacement.rcNormalPosition.Left
    H = wndPlacement.rcNormalPosition.Bottom - wndPlacement.rcNormalPosition.Top

    'reposition calculator form
    ret = MoveWindow(Forms(cszForm).hwnd, r, Y, w, H, True)
    End If

    '************************************************* *****

  4. #4
    Join Date
    Jan 2001
    New York, NY USA
    Thanked 0 Times in 0 Posts

    Re: Using VBA to postion a form on the screen

    Thank you. I picked this up only recently, and I appreciate your sending the information. <img src=/S/salute.gif border=0 alt=salute width=15 height=20>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts