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:
Then, in each form module, I have this 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.
As you can see, the form isn't quite centered, but it's good enough.
Here's another form.
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.
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.
As you can see, the form isn't quite centered, but it's good enough.
Here's another form.
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.