Access – AutoSize SubForm with Main Form Detail Section

I was working on a Form with an embedded subform.

In the Form’s header were a series of controls to perform a search and the subform would display the results to the user.  The issue was that I would setup the design, maximizing the subform within the main form’s detail section, at least within my screen resolution, but when distributed to my users it would present blank area because they had higher resolution/DPI.

Thus, I set out to resize the subform automatically based on the users screen.
 

The VBA Approach

At the end of the day, the required code is very straightforward.  In the main form’s Open event I simply used:

Private Sub Form_Open(Cancel As Integer)
On Error GoTo Error_Handler
    Dim iHeightDelta As Integer 'The change in height from initial design
    Dim iHeaderHeight As Integer
    Dim iFooterHeight As Integer
    
    ' Could use Form_HasSection() instead.
    If Me.Section(acHeader).Visible Then iHeaderHeight = Me.Section(acHeader).Height
    If Me.Section(acFooter).Visible Then iFooterHeight = Me.Section(acFooter).Height
    iHeightDelta = (Me.InsideHeight - iFooterHeight - iHeaderHeight) - Me.Employees.Height - (0.0417 * 1440 * 2)
    Me.Employees.Height = Me.Employees.Height + iHeightDelta
    
Error_Handler_Exit:
    On Error Resume Next
    Exit Sub
 
Error_Handler:
    If Err.Number = 2462 Then
        'Skip issue with sections not exitsing/being visible
        Resume Next
    Else
        MsgBox "The following error has occurred" & vbCrLf & vbCrLf & _
               "Error Source: Form_Open" & vbCrLf & _
               "Error Number: " & Err.Number & vbCrLf & _
               "Error Description: " & Err.Description & _
               Switch(Erl = 0, "", Erl <> 0, vbCrLf & "Line No: " & Erl) _
               , vbOKOnly + vbCritical, "An Error has Occurred!"
        Resume Error_Handler_Exit
    End If
End Sub

Now, I’m sure you wondering what the heck is the (0.0417 * 1440 * 2) all about?  This is simply a small offset/space/buffer I always have around controls and the edge of the subform within the main form.  I want the subform to be spaced 0.0417″ from the top and then I double that so I end up with the same at the bottom (and multiply by 1440 to convert to twips for Access).  That said, if you don’t want such a gap, simply remove that extra subtraction.

Also, as you can see in the code I’m using the error handler to deal with cases when certain sections may not exist. If you prefer, you could use a function like the following

'---------------------------------------------------------------------------------------
' Procedure : Form_HasSection
' Author    : Daniel Pineault, CARDA Consultants Inc.
' Website   : http://www.cardaconsultants.com
' Purpose   : Check if a form section exists, is visible or not
' Copyright : The following is release as Attribution-ShareAlike 4.0 International
'             (CC BY-SA 4.0) - https://creativecommons.org/licenses/by-sa/4.0/
' Req'd Refs: None required
'
' Input Variables:
' ~~~~~~~~~~~~~~~~
' oForm     : Form to check, often Me
' iSection  : Section to check for using the AcSection Enum values
'
' Usage:
' ~~~~~~
' Form_HasSection(Me, acFooter)
'   Returns -> True/False
'
' Revision History:
' Rev       Date(yyyy-mm-dd)        Description
' **************************************************************************************
' 1         2004-09-17              Initial Release
' 2         2024-01-23              Updated Function Header
'---------------------------------------------------------------------------------------
Public Function Form_HasSection(oForm As Form, _
                                iSection As AcSection) As Boolean
    On Error Resume Next
    Form_HasSection = oForm.Section(iSection).Visible
End Function

 

Using A Control’s Anchor Property

As pointed out by Michael Simcich in the comments below, once Microsoft introduced layout view and anchoring (circa Access 2007+) it became possible to use the Horizontal Anchor and/or Vertical Anchor properties to accomplish this. By setting them to ‘Both’ the subform will expand/contract based on the change to the parent form size.
 

Download a Demo Database

Disclaimer/Notes:

If you do not have Microsoft Access, simply download and install the freely available runtime version (this permits running MS Access databases, but not modifying their design):

Microsoft Access 2010 Runtime
Microsoft Access 2013 Runtime
Microsoft Access 2016 Runtime
Microsoft 365 Access Runtime

All code samples, download samples, links, ... on this site are provided 'AS IS'.

In no event will Devhut.net or CARDA Consultants Inc. be liable to the client/end-user or any third party for any damages, including any lost profits, lost savings or other incidental, consequential or special damages arising out of the operation of or inability to operate the software which CARDA Consultants Inc. has provided, even if CARDA Consultants Inc. has been advised of the possibility of such damages.

 
Feel free to download a 100% unlocked demo copy by using the link provided below:
 
Download “AutoSize SubForm” SubFormAutoSize.zip – Downloaded 4419 times – 121.04 KB  

Resources on the Subject

 

Page History

Date Summary of Changes
2021-05-18 Initial Release
2024-01-23 Update code based on testing to be more reliable
2024-01-24 Added YouTube Video
Added Download Sample Database

3 responses on “Access – AutoSize SubForm with Main Form Detail Section

    1. Daniel Pineault Post author

      Yes and no.

      Firstly, the anchor property has not always existed. Using a VBA solution is universal.

      It is definitely a viable option to consider depending on the usage and database format.

      I’ve added a section to the article with regards to this. Thank you for the comment.

  1. Dee Watson

    This helped me. The built-in anchoring doesn’t work when you are moving sub-forms around a main form at run-time e.g. a main form with 3 sub-forms side-by-side where the user can hide the middle sub-form at runtime. The right hand sub-forms didn’t re-anchor in this scenario. So, your code helped. Just needed to adapt it for the width of the main form. Thanks.