Centre controls on the screen

Your Access forms might be running on all sorts of different computers with different sizes of screen. The controls might look good when you design the form but they'll all be crowded on the left-hand side if the user maximises the form on a larger screen.

The code below is my solution. I've kept it simple by restricting the user's choices and making the following assumptions:

  • The form always runs maximised.
  • The user can't minimize or resize the form.
  • Controls stay the same size.
  • Controls are only centred horizontally.

Put this code in the form's Open method:

Private Sub Form_Open(Cancel As Integer)
   Dim intOrigWidth As Integer
   Dim intOffSet As Integer

   '-- Remember the original width of the form
   '-- so that we can calculate the offset required.

   intOrigWidth = Me.WindowWidth
   intOffset = (Me.WindowWidth - intOrigWidth) / 2

   '-- Freeze the screen to prevent flickering
   '-- whilst we rearrange the screen.

   Me.Painting = False
   For Each ctl In Me.Controls
     ctl.Left = ctl.Left + intOffset
   Next ctl

   '-- Show all the changes at once
   Me.Painting = True
End Sub

Set the following properties:

  • Dividing Lines = No.
  • Border Style = None.
  • Min Max buttons = No.

You will end up with a form that looks like something this:

[Controls centred in screen]


A better solution would be to write much more code to resize the controls as the user changes the size of the form. This gets difficult because some controls (like textboxes) must stay the same height whereas others (like listboxes) must get larger as the form gets taller. Restricting myself to horizontal movement kept it simple.

If the form includes containers like Tabbed Controls or Option Groups then you'll need to add extra code to handle them. Use the ControlType property inside the loop to detect these controls.

Access Tips

FoxPro Tips

General Tips


Related Items

Address formats

Storing addresses in a consistent format

Read More

How to design a form to make data entry easier

How to design a form to make data entry easier so that users can enter data more quickly and more accurately

Read More

American date formats used by Access SQL

Microsoft Access uses an American date format in SQL commands

Read More

FoxPro Tips

Top ten tips for Visual FoxPro

Read More

Top ten tips for Microsoft Access

Access Tips and Hints from Alvechurch Data

Read More