Access – VBA – Convert ControlType to Name

If ever you created code to loop over controls to identify their type (.ControlType), if you’re like me, at times, you wanted to report the data in English, not as numbers.

It’s nice to know the ControlType is 104, but what the hell does that truly mean?!

So then you go looking through the Object Browser and eventually review the AcControlType Enum to find out that 104 = “Command Button”.

Now, while it is pleasant to do this manually every time, we need to automate this for our sanity.

Need not worry, we can simply create a simple function like:

'---------------------------------------------------------------------------------------
' Procedure : GetControlTypeName
' Author    : Daniel Pineault, CARDA Consultants Inc.
' Website   : http://www.cardaconsultants.com
' Purpose   : Convert the numeric ControlType value to a Named Control Type
' 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: Late Binding  -> none required
' References: https://learn.microsoft.com/en-us/office/vba/api/access.accontroltype
'
' Input Variables:
' ~~~~~~~~~~~~~~~~
' lControlType  : ControlType value to cross-reference
'
' Usage:
' ~~~~~~
' ? GetControlTypeName(104)
'   Returns -> "Command Button"
'
' Revision History:
' Rev       Date(yyyy-mm-dd)        Description
' **************************************************************************************
' 1         2003-07-12              Initial Release
' 2         2023-02-07              Updated to include 'new' controls
'---------------------------------------------------------------------------------------
Public Function GetControlTypeName(lControlType As Long) As String
On Error GoTo Error_Handler

    Select Case lControlType
        Case acAttachment    '126
            GetControlTypeName = "Attachment"
        Case acBoundObjectFrame    '108
            GetControlTypeName = "Bound Object Frame"
        Case acCheckBox    '106
            GetControlTypeName = "Check Box"
        Case acComboBox    '111
            GetControlTypeName = "Combo Box"
        Case acCommandButton    '104
            GetControlTypeName = "Command Button"
        Case acCustomControl    '119
            GetControlTypeName = "ActiveX"
        Case acEmptyCell    '127
            GetControlTypeName = "Empty Cell"
        Case acImage    '103
            GetControlTypeName = "Image"
        Case acLabel    '100
            GetControlTypeName = "Label"
        Case acLine    '102
            GetControlTypeName = "Line"
        Case acListBox    '110
            GetControlTypeName = "List Box"
        Case acNavigationButton    '130
            GetControlTypeName = "Navigation Button"
        Case acNavigationControl    '129
            GetControlTypeName = "Navigation Control"
        Case acObjectFrame    '114
            GetControlTypeName = "Unbound Object Frame"
        Case acOptionButton  '105
            GetControlTypeName = "Option Button"
        Case acOptionGroup  '107
            GetControlTypeName = "Option Group"
        Case acPage  '124
            GetControlTypeName = "Page"
        Case acPageBreak  '118
            GetControlTypeName = "Page Break"
        Case acRectangle    '101
            GetControlTypeName = "Rectangle"
        Case acSubform  '112
            GetControlTypeName = "SubForm"
        Case acTabCtl    '123
            GetControlTypeName = "Tab"
        Case acTextBox  '109
            GetControlTypeName = "Text Box"
        Case acToggleButton  '122
            GetControlTypeName = "Toggle Button"
        Case acWebBrowser   '128
            GetControlTypeName = "Web Browser"
    End Select
    
Error_Handler_Exit:
    On Error Resume Next
    Exit Function
 
Error_Handler:
    MsgBox "The following error has occurred" & vbCrLf & vbCrLf & _
           "Error Source: GetControlTypeName" & 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 Function

Then it can be used by doing things like

? GetControlTypeName(104)

or

? GetControlTypeName(Ctrl.ControlType)

or

? GetControlTypeName(acCommandButton)


and it will return something like:

Command Button

 

Useful Resources

3 responses on “Access – VBA – Convert ControlType to Name

  1. Steve

    I also include the following:

    ‘ Constants for ControlType of the Screen.ActiveControl.ControlType on a table or query datasheet (note subdatasheets return acSubform as expected)
    Public Const acTableDatasheetControl As Long = 115 ‘ text box, combo box, list box, or check box on a table datasheet returned by Screen.ActiveControl.ControlType
    Public Const acQueryDatasheetControl As Long = 116 ‘ text box, combo box, list box, or check box on a query datasheet returned by Screen.ActiveControl.ControlType

    Steve