Getting pop-up form to center on monitor (1 Viewer)

mcomp72

Member
Local time
Today, 12:38
Joined
Jul 7, 2023
Messages
38
I am creating a new database that I am hoping to eventually make operate like an application. It will be used by my Dad, who doesn't know Access at all.

One thing I am trying to do is create forms (let's call these pop-up forms) that are opened by buttons on other forms. I want the pop-up forms to be centered on the screen when they open. (These pop-up forms have Pop Up set to YES and Modal set to YES.)

I searched around the web and found what I thought was a solution. It kind of works on one of my pop-up forms, but doesn't seem to work very well on the other one.

Below is what I'm doing.

In a general Module, I have the following code at the beginning:

Code:
' Type declarations:
Public Type typRect
    Left As Long
    Top As Long
    Right As Long
    Bottom As Long
End Type

' API declarations:
#If VBA7 Then
    Public Declare PtrSafe Function apiGetClientRect Lib "user32" Alias "GetClientRect" (ByVal hwnd As LongPtr, lpRect As typRect) As Long
    Public Declare PtrSafe Function apiGetWindowRect Lib "user32" Alias "GetWindowRect" (ByVal hwnd As LongPtr, lpRect As typRect) As Long
    Public Declare PtrSafe Function apiSetWindowPos Lib "user32" Alias "SetWindowPos" (ByVal hwnd As LongPtr, ByVal hWndInsertAfter As LongPtr, ByVal x As Long, ByVal y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
    Public Declare PtrSafe Function apiShowWindow Lib "user32" Alias "ShowWindow" (ByVal hwnd As LongPtr, ByVal nCmdShow As Long) As Long
#Else
    Public Declare Function apiGetClientRect Lib "user32" Alias "GetClientRect" (ByVal hwnd As Long, lpRect As typRect) As Long
    Public Declare Function apiGetWindowRect Lib "user32" Alias "GetWindowRect" (ByVal hwnd As Long, lpRect As typRect) As Long
    Public Declare Function 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
    Public Declare Function apiShowWindow Lib "user32" Alias "ShowWindow" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
#End If

' Constant declarations:
Public Const SW_RESTORE = 9
Public Const SWP_NOSIZE = &H1 ' Don't alter the size
Public Const SWP_NOZORDER = &H4 ' Don't change the Z-order
Public Const SWP_SHOWWINDOW = &H40 ' Display the window

Public Function gfncCenterForm(parForm As Form, T As Boolean) As Boolean
'T = Allow the form to center at top of screen

Dim varAccess As typRect, varForm As typRect
Dim varX As Long, varY As Long

On Error GoTo CenterForm_Error
Call apiGetClientRect(hWndAccessApp, varAccess) ' Get the Access client area coordinate
Call apiGetWindowRect(parForm.hwnd, varForm) ' Get the form window coordinates
varX = CLng((varAccess.Left + varAccess.Right) / 2) - CLng((varForm.Right - varForm.Left) / 2) ' Calculate a new left for the form
If T = True Then varY = 80 'M Javes edit 31.03.2020
If T = False Then varY = CLng((varAccess.Top + varAccess.Bottom) / 2) - CLng((varForm.Bottom - varForm.Top) / 2) ' Calculate a new top for the form
varY = varY - 45 ' Adjust top for true center
varY = varY - 20 ' Adjust top for appearance
Call apiShowWindow(parForm.hwnd, SW_RESTORE) ' Restore form window
Call apiSetWindowPos(parForm.hwnd, 0, varX, varY, (varForm.Right - varForm.Left), (varForm.Bottom - varForm.Top), SWP_NOZORDER Or SWP_SHOWWINDOW Or SWP_NOSIZE) ' Set new form coordinates
gfncCenterForm = True
Exit Function

CenterForm_Error:
gfncCenterForm = False
  
End Function

Then, in each form module, I have this sub:

Code:
Private Sub Form_Open(Cancel As Integer)

Call gfncCenterForm(Me, False)

End Sub

I did not write any of the above code. I found it posted on a forum, where the person was sharing how they had made forms center on their computer. (Maybe the code is specific to their computer?)

Here is what it looks like when I open one of the forms.

Screenshot (259).png


As you can see, the form isn't quite centered, but it's good enough.

Here's another form.

Screenshot (258).png


This one is too high up on the screen. I'd really like it to be centered.

Unfortunately, I don't have any experience with API calls in VBA, so I don't know how to figure out why the code isn't working. If anyone has any thoughts, I'd appreciate it.

Or if anyone has another method to make the forms center on the screen, I'm open to trying a completely different approach.
 
I wrote a whole article on this topic some time ago:

The article provides four different sets of code to centre a form. Each gives slightly different results depending on whether the form is a popup or standard form. IIRC, modal doesn't affect the outcomes

Anyway, try them all out and let me know how you get on. Also which works best for you?
 
Thanks for that. I tried the code for Form 4 from the site, and it worked great except it didn't make the forms quite wide enough. Here's what one of them looks like now:

Screenshot (260).png


See how the Cancel button is cut-off and there is now scroll bar at the bottom of the form?

Any suggestion for how I can make the entire form appear without the scroll bar?
 
I decided to try the code from Form 3, just to see if the same problem occurred as I mentioned above, and I when I compiled the code, I received an error. It said the following variable is not defined: MetricsScreenWidth

It looks like MetricsScreenHeight is also not defined.

It doesn't appear either of them are mentioned anywhere else in the code, so I'm guessing other variables were supposed to be here. Any idea which ones?
 
MetricsScreenHeight & MetricsScreenWidth are included in modMetrics which is supplied with both of the sample apps I uploaded.
Both apps are fully compiled.
Perhaps you didn't copy that module to your own app?

If that doesn't fix the with issue, try a different version of the code - I use my own code which is in Form 3
If you still have the same problem, go to design view and widen the form slightly then save
 
Oh, I didn't realize I needed to download the actual file. I copied the code you had on the web page, first for Form 4, and then for Form 3. I didn't realize for Form 3 I needed to get code out of the actual file.

I just opened the file, and I see a discrepancy. I'm wondering if I used the wrong code before for Form 4. On the website it says to use code in module modFormCentre.

Screenshot (262).png


But on the form itself in the file, it says to use the one in the module modCenterForm.

Screenshot (261).png


Can you confirm which one I should be using for Form 4?
 
You can ignore my previous post. Once I got modMetrics imported into my file, the code for Form 3 worked perfectly. Thanks very much!!
 
Excellent. Glad you got it working

Sorry about the lack of clarity in the article which I have just updated
1. The text for form 3 now states that you also need to import modMetrics
2. I've amended the text on form 4 to state that the code for that is in modFormCentre & updated all 6 screenshots

Hopefully all clear now. Thanks for the prompt
 
MetricsScreenHeight & MetricsScreenWidth are included in modMetrics which is supplied with both of the sample apps I uploaded.
Both apps are fully compiled.
Perhaps you didn't copy that module to your own app?

If that doesn't fix the with issue, try a different version of the code - I use my own code which is in Form 3
If you still have the same problem, go to design view and widen the form slightly then save
Hi unfortunately the files on the Isladogs site appear to be not there (i.e. 404 File not found came up)?
 
here is my version. Credit of the code is given.
 

Attachments

@arnlgp
As with the examples in my article, your code gives different 'centre positions' depending on whether the form is a popup (where it appears to work well) or not.
Using a standard non-popup form and overlapping windows, the form is off-centre both horizontally and vertically
 
Using a standard non-popup form and overlapping windows, the form is off-centre both horizontally and vertically
i think it does center the form well.
i'm not sure for "headless" form if it centers.
 
i think it does center the form well.
i'm not sure for "headless" form if it centers.
Yes it does center well for popup forms as I said before.
However the screenshot below is a standard form using overlapping windows display and Access maximized. As you can see its off-centre (as is the case with most of the code I've used in my own article)

1690448205798.png


When Access isn't maximized, the code doesn't allow for that when positioning the form:

1690448455982.png
 
the fix is used Pop-up form whether on Tabbed or Overlapping window.
 
Using a popup is not always desirable!
Your centre form example is amazing, thanks!

I'm currently using your ideas from it it to make a function called JustTheTop to vertically re-position a data sheet view form to be more sensibly in the middle of the screen, I did wonder though, how do you have popupForm3 and Popupform4 both calling CentreForm Me and referring to two different Sub's with the same name? how is that possible without qualifying the module?
 
Hi
Thanks for your comments.
If you look closely you will see I used the UK spelling CentreForm in my own code (used in Form 3) and the US spelling Center Form was used by Renaud Bompuis in his code (used in Form4)

The reason I gave 4 different versions of the code was because there is no clear winner. The outcomes depend on whether:
a) Access is maximised or not (occupies part of the screen only)
b) the navigation pane is maximised/minimised or hidden
c) the forms are popup or not

For anyone new to this thread, the code can be found at
 
Oh wow, I even thought that spelling may be it and read them carefully, I thought.......... clearly should have pasted them one above another! Embarrasing!

I too am discovering this joy, for a long time (nearly 15 years! yikes!) I've used your fAccessWindow "HIDE" to hide the main window and present users a modal pop-up form as "home" followed by further pop-up forms, this worked well, every so often someone would end up with a maximised Access window stuck on a monitor they were trying to copy data back & forth between but it was only once every few years and related to RDP as well so I kinda shrugged it off.

However I recently worked to re-add right click filter options (hard coding tons of my own onto double click etc is tedious, especially when the date ones and "NOT" is so well implemented as standard, I discovered this only worked if ACCESS wasn't hidden, which suddenly trapped my users behind an access main window they couldn't move because of the modal home screen pop-up. Since then I've been plagued by the "auto centre" being tied to the Access window, before now, with Access hidden it was always screen centre and I didn't realise the pain I'd been avoiding,

I resorted to making the "home" form non-pop-up and sizing & positioning the access window sensibly with the nav bar and ribbon hidden, this worked nicely apart from the centering issue,

I'm pretty close with your CentreForm code from Pop-up Form 3 & 4 (I opted for 4 for no specific reason) however, a lot of users have dual screens and it does make good sense to make use of both screens for cross-referencing data for some bits of my application, I'd like to use your monitor aware resize to position vertically but I'm struggling to get the horizontal bit to stay in the same place. I just figured out I could do

Code:
docmd.movesize , (ScreenHeight - formHeight) / 2, formWidth, formHeight

I could skip it by leaving out CurrentLeft as below, but i'd like to understand why windowleft wasn't giving the correct position? when moving a window around and clicking a button I got negatives quite often, for a 2560px screen I'd get e.g.28845 and if i moved further i'd get -23829 it's almost like it was taking the shortest distance measured left or right? or dividing the screen into 65536 and going from -32768 to 32768 ?

Code:
Dim CurrentLeft As Long
CurrentLeft = f.WindowLeft
DoCmd.MoveSize CurrentLeft, (ScreenHeight - formHeight) / 2, formWidth, formHeight

Additionally I'm finding continuous view forms still hang off the bottom of the screen half the time, is this because Access is struggling to identify how long they are? Will resize option 3 do a better job of understanding their height and positioning them in the vertical centre? Thanks
 
Last edited:

Users who are viewing this thread

Back
Top Bottom