AutoCenter with code

  • Thread starter Thread starter Del J
  • Start date Start date
D

Del J

I have a form that allows the user change its size with a More/Less command
button. How can I recenter the form in the application window as it changes
size?
 
Del J said:
I have a form that allows the user change its size with a More/Less command
button. How can I recenter the form in the application window as it changes
size?

Hi Del

I did this using a class module. I used Nicole Calinoiu's clFormWindow class
to do this sort of thing.
It makes it quite easy.

http://www.mvps.org/access/forms/frm0042.htm
Forms: Move and Resize form windows from code

Remember to save it as a class module, not an ordinary module.


Code is below.

' Center or Top Left
If Forms!frmTitlePage!chkCentre = True Then
DoCmd.MoveSize 0, 0
Else
subFormCentre (strFormName)
End If

frmTitlePage is the switchboard and chkCentre is a checkbox they tick to
place a form at the top left hand corner. If you only want it to be
centred, just use the subFormCentre(strFormName) line and put in the form
name as strFormName.

subFormCentre is:

Public Sub subFormCentre(ByVal strFormName As String)
Dim fwForm As New clFormWindow
Const SMALL_OFFSET = 5 'Used to position window slightly away from the
Access MDI window border _
in order to avoid appearance of the Access window
vertical scroll bar.
On Error GoTo Error_subFormCentre

With fwForm
..hWnd = Forms(strFormName).hWnd
..Top = (.Parent.Height - .Height) / 2
..Left = (.Parent.Width - .Width - SMALL_OFFSET) / 2
End With

Set fwForm = Nothing

Exit_subFormCentre:
On Error GoTo 0
Exit Sub

Error_subFormCentre:
MsgBox "An unexpected situation arose in your program." & funCrLf & _

Resume Exit_subFormCentre

End Sub

Neville Turbit
www.projectperfect.com.au
 
Back
Top