If you use Overlapping Windows (see screenshot below) in your Access application, you need to think about where you want each form to appear when the user opens it.
Here are six techniques you can use to control where forms appear.
Techniques Listed By Form Open Location
Open in the Center of the Screen
Set the form's AutoCenter property to True:
- Auto Center: Yes
Open in the Upper-Left of the Screen
Use the DoCmd.MoveSize method to relocate the form to the top left corner of the canvas by setting the Right and Down arguments to
DoCmd.MoveSize 0, 0
This line of code can be placed in one of several places:
- After the
DoCmd.OpenFormline in the calling code
- In the
- In the
Open to the Right of the Active Form
To display the newly opened form side-by-side with the active form, we can use the active form's Width and Left properties to calculate the Right and Down arguments of the MoveSize method:
Dim Right As Integer, Down As Integer
Right = Screen.ActiveForm.WindowLeft + Screen.ActiveForm.WindowWidth
Down = Screen.ActiveForm.WindowTop
DoCmd.MoveSize Right, Down
You can use variations of the following form window positioning properties (WindowLeft, WindowWidth, WindowTop, WindowHeight) to align any two windows relative to each other, but opening a child window to the immediate right of a parent window is the most common scenario in my experience.
Open to an Arbitrary Size and Location
To control exactly where and how big a form is when it opens on-screen, you can position the form and save it in Design View.
Here are the key steps:
- Verify the following form properties:
Auto Center: No
Auto Resize: No
Fit to Screen: No
- Open the form in Design View
- Size and place the form where you want it (relative to the top left corner of the screen)
- Save and close the form
The next time the form is opened, it will open to the same size and location as where you saved it, assuming there is enough space on the Access canvas.
Open to the Last Used Location
Here's the basic idea:
- In the
Form_Unloadevent, save the form's Top, Left, Height, and Width in the user registry via the SaveSetting statement
- In the
Form_Loadevent, load the form's Top, Left, Height, and Width settings from the user registry via the GetSetting function
DoCmd.MoveSizewith the retrieved positions
This is something that could easily be generalized across an entire application.
You can use my
FillAccessWindow() function to position forms as a percentage of the Access canvas.
The screenshot below was created with three lines of code:
'Create a sidebar with stacked forms in the remaining space
DoCmd.OpenForm "Form1":FillAccessWindow , ,"NW", 1, .4
DoCmd.OpenForm "Form2":FillAccessWindow , ,"NE", .5, .6
DoCmd.OpenForm "Form3":FillAccessWindow , ,"SE", .5, .6
Pitfalls and Caveats
The Access "Canvas"
I mentioned the Access "canvas" several times in the above techniques. The canvas is the main area of the Access window where forms and reports are displayed. When you maximize a form or report it fills the Access canvas.
Here's a helpful visual:
With all of the techniques above, forms position themselves when they open relative to the Access canvas. However, for popup windows, those positions are relative to the Windows desktop.
Popup forms are positioned relative to the Windows desktop.
There are two primary ways to open a form in popup mode:
- Set the form's Pop Up property to Yes in design view
- Pass acDialog to the WindowMode parameter of the DoCmd.OpenForm method
Don't Assume Access is Running Full-Screen
When I'm developing, I tend to leave the Access application maximized to occupy the full screen.
It's convenient for me as a developer, but what if a user doesn't want to work that way? What if they have their main Access window restored to a quarter of the available screen real estate? If you've created a bunch of prepositioned forms while Access is maximized, those forms may get resized or lost off screen when opened on a user's smaller Access canvas.
One way to deal with this is to use my
Don't Assume Your Screen Resolution is Typical
Even if you tell your users they must run Access full screen, their full screen may not be the same as your full-screen. This is especially true with the proliferation of high-DPI displays.
To account for this, I do my best to learn the smallest resolution among my users. I then use an AutoHotKey script I wrote to resize my own Access application to fit into one of a number of pre-configured screen resolutions.