'Setting the location of a form in access vba

I'm working on an Access application with multiple nested forms on which the users clicks on selections in order to do specific tasks, e.g., enter new data, search for data, print a report, etc.

Currently I have each form set with autocenter. I would like the user to be able to move the main form, then have all other forms open on top of the main form. The forms typically open as a dialog.

I've just added the code to allow the subsequent forms to open in the same location as the main form. The main forms has code that looks like this:

Private Sub File_Maint_Click()
Dim ObjName As String
FrmWinTop = Forms![Main Form].WindowTop
FrmWinLeft = Forms![Main Form].WindowLeft

ObjName = "File Maintenance Form"
If IsSuper Then DoCmd.OpenForm ObjName
If Not IsSuper Then DoCmd.OpenForm ObjName, acNormal, , , , acDialog

End Sub

Each subsequent form has code like this:

Private Sub Form_Open(Cancel As Integer)

    Me.Move FrmWinLeft, FrmWinTop

End Sub

This works great, except - in normal operation I have the Access window minimized by code that opens the main form, and then the top and left of the main form is some obscure number, such that when the subsequent form opens it is not visible. If the Access window is visible, the code works correctly.

Apparently my method won't work with the Access window minimized and only the dialog forms show, as far as I can tell. Is there a different way to hide the Access window when the application is running that would allow the forms to be located correctly? Or is there a different method of locating the forms on the screen such that each subsequent form will cover the previous form that opened it?

Thanks much for reading my question, and providing any thoughts you may have.



Solution 1:[1]

Move these variables to a module as Global/Public variables:

Public FrmWinTop  As Long
Public FrmWinLeft As Long

Then set them using the OnActivate event of the main form:

FrmWinTop = Me.WindowTop
FrmWinLeft = Me.WindowLeft

Solution 2:[2]

I found the following code at http://www.access-programmers.co.uk/forums/showthread.php?t=218554:

Function fSetAccessWindow(nCmdShow As Long)

Dim loX As Long
Dim loForm As Form
On Error Resume Next
Set loForm = Screen.ActiveForm

    If Err <> 0 Then
        loX = apiShowWindow(hWndAccessApp, nCmdShow)
        Err.Clear
    End If

    If nCmdShow = SW_SHOWMINIMIZED And loForm.Modal = True Then
        MsgBox "Cannot minimize Access with " _
        & (loForm.Caption + " ") _
        & "form on screen"
    ElseIf nCmdShow = SW_HIDE And loForm.PopUp <> True Then
        MsgBox "Cannot hide Access with " _
        & (loForm.Caption + " ") _
        & "form on screen"
    Else
        loX = apiShowWindow(hWndAccessApp, nCmdShow)
    End If
    fSetAccessWindow = (loX <> 0)

End Function

If I use this method to hide the main Access window as opposed to setting it as minimized, the location values seem to work correctly. Guess I should have looked around a bit more before posting a question.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Gustav
Solution 2 BlairH