CodeContextObject / Converted Macro

imodett 61 Reputation points
2021-03-31T17:47:13.377+00:00

I receive the following error:

Run-Time error 2467
The Expression you entered refers to an object that is closed or doesn’t exist.

And the following line is highlighted in my code:

Set objCurrent = CodeContextObject

Can anyone assist with me with finding the solution?

Here is the code. The code has been converted from a macro.

Option Compare Database
Option Explicit

'------------------------------------------------------------
' Search
'
'------------------------------------------------------------
Function Search()

Dim objCurrent As Object
Set objCurrent = CodeContextObject

    On Error GoTo Search_Error
10        With CodeContextObject.Name
20            If (Eval("[Form]![SearchBox] Is Null Or [Form]![SearchBox]=""""")) Then
                  ' Clear Filter when search box empty
30                DoCmd.ApplyFilter "", """""", ""
40                DoCmd.GoToControl "SearchBox"
50                DoCmd.SetProperty "cmdSearchClear", acPropertyVisible, "0"
60                DoCmd.SetProperty "iconSearchClear", acPropertyVisible, "0"
70                DoCmd.SetProperty "cmdSearchGo", acPropertyVisible, "-1"
80                DoCmd.SetProperty "iconSearchGo", acPropertyVisible, "-1"
90            End If
100           If (Eval("[CurrentProject].[IsTrusted] And ([Form]![SearchBox] Is Null Or [Form]![SearchBox]="""")")) Then
110               .SearchBox.Text = ""
120           End If
130           If (Eval("[Form]![SearchBox] Is Null Or [Form]![SearchBox]=""""")) Then
140               End
150           End If
160           If (VarType(.Form!SearchBox) <> 8) Then
170               End
180           End If
190           DoCmd.SetProperty "cmdSearchGo", acPropertyVisible, "-1"
200           DoCmd.SetProperty "iconSearchGo", acPropertyVisible, "-1"
210           If (Eval("([Form]![SearchBox] Is Null Or [Form]![SearchBox]="""") And [cmdSearchClear].[Visible]<>0")) Then
220               DoCmd.SetProperty "cmdSearchClear", acPropertyVisible, "0"
230               DoCmd.SetProperty "iconSearchClear", acPropertyVisible, "0"
240               End
250           End If
              ' Handle "'s in search
260           TempVars.Add "strSearch", Replace(.Form!SearchBox, """", """""")
              ' Build the Filter for the Task list
270           If (.Form.Name = "Task List") Then
280               TempVars.Add "strFilter", "([Title] Like "" * " & [TempVars]![strSearch] & " * "" )"
290           End If
300           If (.Form.Name = "Task List") Then
310               TempVars.Add "strFilter", TempVars!strFilter & " OR ([Description] Like "" * " & [TempVars]![strSearch] & " * "" )"
320           End If
330           If (.Form.Name = "Task List") Then
340               TempVars.Add "strFilter", TempVars!strFilter & " OR ([Status] Like "" * " & [TempVars]![strSearch] & " * "" )"
350           End If
360           If (.Form.Name = "Task List") Then
370               TempVars.Add "strFilter", TempVars!strFilter & " OR ([Priority] Like "" * " & [TempVars]![strSearch] & " * "" )"
380           End If
              ' Build the Filter for the Contact list
390           If (.Form.Name = "Contact List") Then
400               TempVars.Add "strFilter", "([Last Name] Like "" * " & [TempVars]![strSearch] & " * "" )"
410           End If
420           If (.Form.Name = "Contact List") Then
430               TempVars.Add "strFilter", TempVars!strFilter & " OR ([First Name] Like "" * " & [TempVars]![strSearch] & " * "" )"
440           End If
450           If (.Form.Name = "Contact List") Then
460               TempVars.Add "strFilter", TempVars!strFilter & " OR ([E-mail Address] Like "" * " & [TempVars]![strSearch] & " * "" )"
470           End If
480           If (.Form.Name = "Contact List") Then
490               TempVars.Add "strFilter", TempVars!strFilter & " OR ([Company] Like "" * " & [TempVars]![strSearch] & " * "" )"
500           End If
510           If (.Form.Name = "Contact List") Then
520               TempVars.Add "strFilter", TempVars!strFilter & " OR ([Job Title] Like "" * " & [TempVars]![strSearch] & " * "" )"
530           End If
540           If (.Form.Name = "Contact List") Then
550               TempVars.Add "strFilter", TempVars!strFilter & " OR ([Notes] Like "" * " & [TempVars]![strSearch] & " * "" )"
560           End If
570           If (.Form.Name = "Contact List") Then
580               TempVars.Add "strFilter", TempVars!strFilter & " OR ([Zip/Postal Code] Like "" * " & [TempVars]![strSearch] & " * "" )"
590           End If
              ' Apply the Filter
600           DoCmd.ApplyFilter "", TempVars!strFilter, ""
610           TempVars.Remove "strFilter"
620           TempVars.Remove "strSearch"
630           DoCmd.SetProperty "cmdSearchClear", acPropertyVisible, "-1"
640           DoCmd.SetProperty "iconSearchClear", acPropertyVisible, "-1"
650           DoCmd.GoToControl "SearchBox"
660           DoCmd.SetProperty "cmdSearchGo", acPropertyVisible, "-1"
670           DoCmd.SetProperty "iconSearchGo", acPropertyVisible, "-1"
680       End With




    On Error GoTo 0
    Exit Function

Search_Error:

    MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure Search, line " & Erl & "."

End Function
Access Development
Access Development
Access: A family of Microsoft relational database management systems designed for ease of use.Development: The process of researching, productizing, and refining new or existing technologies.
895 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. DBG 2,301 Reputation points
    2021-03-31T19:18:01.183+00:00

    Just curious, did that code use to work before? What is the purpose of that code? What do you expect CodeContextObject to be?


  2. Gustav 717 Reputation points MVP
    2025-01-19T16:58:23.1033333+00:00

    CodeContextObject works inside a form or report:

    Option Compare Database
    Option Explicit
    
    
    Private Sub Command1_Click()
    
        On Error GoTo Command1_Err
    
        Error 11 ' Raise 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
    
        
    Private Function ErrorMessage( _
        strText As String, _
        intType As Integer, _
        intErrVal As Integer) _
        As Integer
        
        Dim objCurrent As Object
        Dim strMsgboxTitle As String
    
        Set objCurrent = Application.CodeContextObject
    
        strMsgboxTitle = "Error in " & objCurrent.Name
        strText = strText & "Error #" & intErrVal & " occurred in " & objCurrent.Name
        ErrorMessage = MsgBox(strText, intType, strMsgboxTitle)
        err = 0
    
    End Function
    

    enter image description here

    Source: Application.CodeContextObject property (Access)

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.