Application.CodeContextObject (Access)
You can use the CodeContextObject property to determine the object in which a macro or Visual Basic code is executing.
The CodeContextObject property is set by Microsoft Access and is read-only in all views. The ActiveControl, ActiveDatasheet, 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.
Private Sub Command1_Click()
On Error GoTo Command1_Err
Error 11 ' Generate divide-by-zero error.
Exit Sub
Command1_Err:
If ErrorMessage("Command1_Click() Event", vbYesNo + _
vbInformation, Err) = vbYes Then
Exit Sub
Else
Resume
End If
End Sub
Function 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 = 0
End Function