Microsoft Access Validate If A Form Section Exists

As part of my recent Class Module articles I needed to use a function to validate the existence of form sections:

  • Header
  • Detail
  • Footer

I thought I’d share a very simple procedure to do just that!


The simplest way to do this is to simply attempt to access the section in question. If it exists, then the code will work, but if it errs, then the section does not exists. Knowing this and using basic error handling, we can develop a simple routine like:

'---------------------------------------------------------------------------------------
' Procedure : Frm_SectionExists
' Author    : Daniel Pineault, CARDA Consultants Inc.
' Website   : http://www.cardaconsultants.com
' Purpose   : Validates if a Form sections exists or not
'               True  => Section exists
'               False => Section does not exist
' 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:
' ~~~~~~~~~~~~~~~~
' frm       : form object (Me)
' lSection  : acSection enum representing the section to validate the existence of
'
' Usage:
' ~~~~~~
' Frm_SectionExists(Me, acHeader)
'   Returns -> true/False
'
' Revision History:
' Rev       Date(yyyy-mm-dd)        Description
' **************************************************************************************
' 1         2011-09-04
' 2         2020-11-21              Updated procedure header
' 3         2024-02-24              Removed redundant SectionExists = True line of code
'---------------------------------------------------------------------------------------
Function Frm_SectionExists(frm As Access.Form, _
                       lSection As AcSection) As Boolean
    On Error GoTo Error_Handler

    Frm_SectionExists= frm.Section(lSection).Visible

Error_Handler_Exit:
    On Error Resume Next
    Exit Function

Error_Handler:
    Resume Error_Handler_Exit
End Function

and to use it we simply need to do something like:

If Frm_SectionExists(Me, acDetail) Then
        Me.Section(acDetail).BackColor = RGB(224, 224, 224)
    End If