Access Cookbook - Ken Getz [47]
DoCmd.MoveSize , , acbcMinWidth
intWidth = Me.InsideWidth
End If
If intHeight < acbcMinHeight Then
DoCmd.MoveSize , , , acbcMinHeight
intHeight = Me.InsideHeight
End If
' Set the detail section's height to be the same as the form's.
' Change this if you want to include header and footer sections.
Me.Section(0).Height = intHeight
' Align all the other controls, based on the left margin of the text box.
Set ctl = Me.txtEntry
With ctl
' Make the left and bottom margins equal.
.Height = intHeight - (.Left + .Top)
' The new width is the width of the form, minus the width of the
' buttons, minus 3 times the gap (the left margin). Two gaps are
' for the buttons, and one more is for the left margin itself.
.Width = intWidth - Me.cmdOK.Width - (3 * .Left)
End With
' Set the positions of the two buttons.
With Me.cmdOK
.Left = intWidth - .Width - ctl.Left
End With
With Me.cmdCancel
.Left = intWidth - .Width - ctl.Left
End With
ExitHere:
Exit Sub
HandleErr:
fInHere = False
Resume ExitHere
End Sub
Discussion
The code used in this solution reacts to the Resize events that occur when you resize a form in run mode (and when you open the form). The code retrieves the form's current size (its InsideWidth and InsideHeight properties) and resizes the controls accordingly.
This example starts out by checking a flag, fInHere, and causes the subroutine to exit if the variable's value is True. It's possible that the procedure itself might cause another Resize event (if you've sized the form smaller than the preset minimum size); this flag ensures that the routine doesn't do more work than it needs to do.
USING THE STATIC KEYWORD
The fInHere flag was declared with the Static keyword. This keyword indicates that Access will maintain the value of the variable between calls to the function. You could accomplish the same effect by making fInHere global, but making the variable static makes it exist as long as the form is loaded, maintains its value from one call to another, and is local to the current procedure. The variable performs its task (as a sentry) without possible intervention from any other procedure.
The code next retrieves the current form size and stores the values into local variables. By placing these values into variables, Access eliminates the need to retrieve the values of the properties every time you need to use them. This speeds up the operation, because retrieving property values is expensive in terms of operating speed.
' Get the current screen coordinates.
intHeight = Me.InsideHeight
intWidth = Me.InsideWidth
Once it has retrieved the sizes, the procedure verifies that the form hasn't been sized too small by the user. If it has been, it forces the form to be at least as large as the preset values of acbcMinWidth and acbcMinHeight:
If intWidth < acbcMinWidth Then
DoCmd.MoveSize , , acbcMinWidth
intWidth = Me.InsideWidth
End If
If intHeight < acbcMinHeight Then
DoCmd.MoveSize , , , acbcMinHeight
intHeight = Me.InsideHeight
End If
Finally, the procedure sets the sizes and locations of each of the controls based on the new width and height of the form. First, it sets the height of the form's detail section, Section(0), so that there will be room for all of the controls at the new height. It then sets the width and height of the text box and sets the left coordinates of the command buttons. This preserves their sizes but resets their positions:
Set ctl = Me.txtEntry
With ctl
.Height = intHeight - (.Left + .Top)
.Width = intWidth - Me.cmdOK.Width - (3 * .Left)
End With
' Set the positions of the two buttons.
With Me.cmdOK
.Left = intWidth - .Width - ctl.Left
End With
With Me.cmdCancel
.Left = intWidth - .Width - ctl.Left
End With
The values used as offsets in this example were all arbitrarily chosen. They work for this particular example, but you'll need to vary them for your own forms. Remember, also, that this example was quite simple. You'll be doing many more calculations if you want to resize a multicolumn list box, for example.