MS Access – VBA – Form Captions

I typically do not add form captions, mainly because I will include a label in the form header (and I find it redundant to repeat it in the form caption). That said, Access in its’ infinite (should I say finite) wisdom figures that since you did not specify a caption (left it blank) then it will help us out by simply displaying the form’s name.

Now, in many instances this is not a big deal, and it can actually be helpful. However, when deploying a database to an end-user, such information can help malicious users to hack your application since they formally know the names of your database objects! As such, I wanted a simple and fast way to solve this issue. The solution, enter a ‘space’ in each form’s caption, thus displaying nothing at all (just what I wanted originally). So I wrote a simple function which quickly loops through all the forms within a database and enters a ‘space’ as the caption for any forms that do not have a caption specified (I wanted to leave any caption I had specifically entered intact!).

 

'---------------------------------------------------------------------------------------
' Procedure : BlankFrmCaptions
' Author    : Daniel Pineault, CARDA Consultants Inc.
' Website   : http://www.cardaconsultants.com
' Purpose   : Loop through all the forms within a database and enters a 'space' as the
'             caption for any forms that do not have a caption specified but leave intact
'             any captions I had specifically entered to avoid displaying the form's
'             name by default.
' Copyright : The following may be altered and reused as you wish so long as the
'             copyright notice is left unchanged (including Author, Website and
'             Copyright).  It may not be sold/resold or reposted on other sites (links
'             back to this site are allowed).
'
' Usage:
' ~~~~~~
' Call BlankFrmCaptions
'
' Revision History:
' Rev       Date(yyyy/mm/dd)        Description
' **************************************************************************************
' 1         2014-Jan-23                 Initial Release
'---------------------------------------------------------------------------------------
Function BlankFrmCaptions()
On Error GoTo Error_Handler
    Dim obj             As AccessObject
    Dim frm             As Form
    Dim sFrm            As String

    For Each obj In CurrentProject.AllForms    'Loop through all the database forms
        sFrm = obj.Name
        DoCmd.OpenForm sFrm, acDesign   'Open the current form in design mode
        Set frm = Forms(sFrm).Form
        If Len(Trim(frm.Caption)) = 0 Then frm.Caption = " "    'Create a blank caption
        DoCmd.Close acForm, sFrm, acSaveYes    'close and save the change
    Next obj    'repeat with the next form until we have processed all of them

Error_Handler_Exit:
    On Error Resume Next
    'Cleanup our variable (better safe than sorry)
    Set frm = Nothing
    Set obj = Nothing
    Exit Function

Error_Handler:
    MsgBox "The following error has occurred." & vbCrLf & vbCrLf & _
            "Error Number: " & Err.Number & vbCrLf & _
            "Error Source: BlankFrmCaptions" & vbCrLf & _
            "Error Description: " & Err.Description, _
            vbCritical, "An Error has Occurred!"
    Resume Error_Handler_Exit
End Function