The ControlCaptionGet Function

Public Function ControlCaptionGet( _
    frmForm As Form, _
    ctlControl As Control _
    ) As String

On Error GoTo Err_Routine

    'Copyright (c) Brendan Reynolds/Timarco Ltd, 1999.
    'All rights reserved.
    
    'e-mail [email protected]

    'This function returns the caption of the label attached
    'to the contol passed in the ctlControl argument on the
    'form passed in the frmForm argument. If ctlControl is
    'itself a label, the function returns the caption of that
    'label. If the control does not have an attached caption,
    'the function returns an empty string.
    
    'Use:
    
    'strCaption = ControlCaptionGet( _
    '   Forms!SomeForm, _
    '   Forms!SomeForm!SomeControl _
    ')
    
    'Or from within the form in question:
    'strCaption = ControlCaptionGet(Me,Me!SomeControl)
    
    Dim lngErrNum As Long
    Dim ctl As Control
    
    ControlCaptionGet = vbNullString
    
    If TypeOf ctlControl Is Label Then
        ControlCaptionGet = ctlControl.Caption
    Else
        For Each ctl In frmForm.Controls
            If TypeOf ctl Is Label Then
                If ctl.Parent.Name = ctlControl.Name Then
                    ControlCaptionGet = ctl.Caption
                    Exit For
                End If
            End If
        Next ctl
    End If
    
Exit_Routine:
    Exit Function
    
Err_Routine:
    lngErrNum = Err.Number
    Select Case Err
        '
    Case Else
        Err.Raise lngErrNum
        Resume Exit_Routine
    End Select

End Function

Download this function in plain text format, ready for import into your own Microsoft Access 97 or Microsoft Access 2000 application, here.

home