CodeContextObject Property

Applies To

Application Object.

Description

You can use the CodeContextObject property to determine the object in which a macro or Visual Basic code is executing.

Setting

The CodeContextObject property is set by Microsoft Access and is read only.

Remarks

The ActiveForm and ActiveReport properties of the Screen object always return the object that currently has the focus. The object with the focus may or may not be the object where a macro or Visual Basic code is currently running, for example, when Visual Basic code runs in the Timer event on a hidden form.

See Also

ActiveForm Property, ActiveReport Property, Application Object, Me Property, Screen Object.

Example

In this example the CodeContextObject property is used in a function to identify the name of the object in which an error occurred. The object name is then used in the message box title as well as in the body of the error message. The Error statement is used in the Command1_Click() event to generate the error for this example.


Private Sub Command1_Click()
    On Error GoTo Command1_Err
    Error 11            ' Generate divide by zero error.
    Exit Sub_Err:
    If ErrorMessage("Command1_Click() Event", vbYesNo + _
    vbInformation, Err) = vbYes Then 
        Exit Sub
    Else
        Resume
    End IfSub
ErrorMessage(strText As String, intType As Integer, _
        intErrVal As Integer) As Integer
    Dim objCurrent As Object
    Dim strMsgboxTitle As String
    Set objCurrent = CodeContextObject
    strMsgboxTitle = "Error in " & objCurrent.Name
    strText = strText & "Error #" & intErrVal & _
        " occurred in " &     objCurrent.Name
    ErrorMessage = MsgBox(strText, intType, strMsgboxTitle)
    Err = 0Function