dsstao
asked on
Centering a Form using VBA
Hi all,
I'm using Access 2007 and I'm attempting to address an issue with a continuous form seemingly taking whatever height it feels like when I open it (and changes on different PCs). I've found a tentative solution in VBA that works great but has a drawback - everything goes to the left.
Here's what I have in the form's Activate event:
Me.Move Left:=10, Top:=10, Height:=7500
This addresses the height issue perfectly. No matter what state I leave the window in while I'm designing, it always gets sized to 7500 twips upon Load. The bad part is, I have no way of knowing the total screen width and therfore cannot set the "Left" property above to a valid number to get it centered. I've tried all different combinations of Screen.Width, etc. including "Me.Move Left:=((Screen.Width / 2) - (Me.Width / 2))" but it just doesn't work (it doesn't move at all). Setting the form's property to Auto Center doesn't work either - this is overridden by the VBA code.
Any ideas?
Thanks,
David
I'm using Access 2007 and I'm attempting to address an issue with a continuous form seemingly taking whatever height it feels like when I open it (and changes on different PCs). I've found a tentative solution in VBA that works great but has a drawback - everything goes to the left.
Here's what I have in the form's Activate event:
Me.Move Left:=10, Top:=10, Height:=7500
This addresses the height issue perfectly. No matter what state I leave the window in while I'm designing, it always gets sized to 7500 twips upon Load. The bad part is, I have no way of knowing the total screen width and therfore cannot set the "Left" property above to a valid number to get it centered. I've tried all different combinations of Screen.Width, etc. including "Me.Move Left:=((Screen.Width / 2) - (Me.Width / 2))" but it just doesn't work (it doesn't move at all). Setting the form's property to Auto Center doesn't work either - this is overridden by the VBA code.
Any ideas?
Thanks,
David
ASKER
It's up there, and I am using non-tabbed (overlapping) windows. Here's a direct link: https://filedb.experts-exchange.com/incoming/ee-stuff/3110-ee-form-position-problem.zip (Note, I simply created a brand new test db with only 1 form demonstrating the problem). Thanks.
If you Set Auto Size to Yes, Auto Center to Yes and Fit To Screen to No, does that get you what you were looking for?
I've worked on something exactly similar to this. My end result discovery is that you can't do much about the centering because of the computer setting for each user.
It has to do with the screen resolution setting for the computer. For example, the window positioning will be different for a 1280 x 800 pixels settings versus a 1024 x 768 pixels. So for your code, 10 Twips is a different position for different resolutions.
But I do have a center code that I think might suffice enough.
It has to do with the screen resolution setting for the computer. For example, the window positioning will be different for a 1280 x 800 pixels settings versus a 1024 x 768 pixels. So for your code, 10 Twips is a different position for different resolutions.
But I do have a center code that I think might suffice enough.
Okay! Here is the centering code that I used on one of my Db and it works fine except when the screen resolution is changed dramatically:
'Auto resize the form window to fit all the items on the form
Dim intWindowHeight As Integer
Dim intWindowWidth As Integer
Dim intTotalFormHeight As Integer
Dim intTotalFormWidth As Integer
Dim intHeightHeader As Integer
Dim intHeightDetail As Integer
Dim intHeightFooter As Integer
' Determine form's height.
intHeightHeader = Form_frmOrg.Section(acHead er).Height
intHeightDetail = Form_frmOrg.Section(acDeta il).Height
intHeightFooter = Form_frmOrg.Section(acFoot er).Height
intTotalFormHeight = CInt(intHeightHeader + intHeightDetail + intHeightFooter)
' Determine form's width.
intTotalFormWidth = Form_frmOrg.Width
' Determine window's height and width.
intWindowHeight = Form_frmOrg.InsideHeight
intWindowWidth = Form_frmOrg.InsideWidth
If intWindowWidth <> intTotalFormWidth Then
Form_frmOrg.InsideWidth = intTotalFormWidth
End If
If intWindowHeight <> intTotalFormHeight Then
Form_frmOrg.InsideHeight = intTotalFormHeight
End If
'Auto resize the form window to fit all the items on the form
Dim intWindowHeight As Integer
Dim intWindowWidth As Integer
Dim intTotalFormHeight As Integer
Dim intTotalFormWidth As Integer
Dim intHeightHeader As Integer
Dim intHeightDetail As Integer
Dim intHeightFooter As Integer
' Determine form's height.
intHeightHeader = Form_frmOrg.Section(acHead
intHeightDetail = Form_frmOrg.Section(acDeta
intHeightFooter = Form_frmOrg.Section(acFoot
intTotalFormHeight = CInt(intHeightHeader + intHeightDetail + intHeightFooter)
' Determine form's width.
intTotalFormWidth = Form_frmOrg.Width
' Determine window's height and width.
intWindowHeight = Form_frmOrg.InsideHeight
intWindowWidth = Form_frmOrg.InsideWidth
If intWindowWidth <> intTotalFormWidth Then
Form_frmOrg.InsideWidth = intTotalFormWidth
End If
If intWindowHeight <> intTotalFormHeight Then
Form_frmOrg.InsideHeight = intTotalFormHeight
End If
Sorry! The code above is just to trim the form. I also set the Auto Center property to True
ASKER
stevbe:
I set the properties as you asked and it did not make a difference. As I mentioned in my original post, the Auto Center does not work as it's overridden by the Me.Move vba code. I did upload the test accdb, feel free to experiment with that yourself. I am living oversees presently and it may take a while to get responses.
I set the properties as you asked and it did not make a difference. As I mentioned in my original post, the Auto Center does not work as it's overridden by the Me.Move vba code. I did upload the test accdb, feel free to experiment with that yourself. I am living oversees presently and it may take a while to get responses.
maybe the MoveSize would work better for you than the Move
from the help file ...
You must include at least one argument for the MoveSize method. If you leave an argument blank, the current setting for the window is used.
So if all you really need to do is set the height then do not set the other arguments ... and you really only need to do this in the Form_Open, if you do it on every Activatre event that coudl be confusing to users as they try to switch back you that form and it moves, no matter where they put it, it moves.
DoCmd.Move Height:=7500
from the help file ...
You must include at least one argument for the MoveSize method. If you leave an argument blank, the current setting for the window is used.
So if all you really need to do is set the height then do not set the other arguments ... and you really only need to do this in the Form_Open, if you do it on every Activatre event that coudl be confusing to users as they try to switch back you that form and it moves, no matter where they put it, it moves.
DoCmd.Move Height:=7500
ASKER
The DoCmd.Move method requires the Left parameter.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I accepted the MoveSize answer with a grading of B. It technically addressed the first part of my question, which was sizing the form without setting a left parameter, but the spirit of the question was "how do I get this form centered and at a height appropriate to each user's screen". The DoCmd.MoveSize command addresses the height issue, but this will immediately cause a problem of too much space above/below the form on displays with higher resolutions or part of the form dissappearing beneath the window on displays with lower resolutions. Ideally, I would have liked to know simply how to center a form regardless of Access window size as well as setting the height based on that size.
However, I appreciate the help and will consider this question closed.
However, I appreciate the help and will consider this question closed.
In the spirit of trying to find a fairly easy work around (as opposed to using win 32 api calls) I wonder if you could open a form hidden, maximize it and then use that form's .InsideHeight to get the number of twips that you could then use in your .MoveSize
What do you have the Document Windows Option set to?