Search code examples
ms-accessvba

Access Run-time Error 2475 *occasionally* occuring


I have an error log that logs in the access table whenever a runtime error occurs for a user in the error trapper, and a particular error seems to occur for 10 random users, every hour at least.

This error appears to occur completely at random, on a random module with the Set ActiveForm code, with random users at random intervals. As far as I can see, there is no pattern between the users.

2475 - "You entered an expression that requires a form to be the active window".

This appears to occur in any of the modules that contain any of the setting of a form. I am using the following lines:

Dim af as Object
Set af = Screen.ActiveForm

I have tried using alternatives, such as declaring it as Form, and also tried the below:

Dim sstatus as String
Dim ps as String

If DLookup("[TM_UserType]", "[SD_Teams]", "[TM_username]= '" & usernm & "'") = "adj" Then
    sstatus = "adj"
Else
    sstatus = "tm"
End If

ps = "frmProdSubmit_" & sstatus

Then referencing the form this way:

Forms(ps).cmbTeam.Value = ""

But this still causes the same issue, even removing the ActiveForm part.

The last thing to mention (as I believe they could be factors) is that the front end is accessed through a shortcut, which minimises the Access window. Not sure if this could be the culprit, or if the user clicking another application can remove the focus.

The back-end of the database is also accessed by up to around 700 users each day.

As it stands, the error trapper pops up with the message, but the front end continues working correctly. It's just an annoying issue to resolve, but am slowly running out of ideas now, and any help would be hugely appreciated!


Solution

  • Error 2475 is thrown when a non-form object is the active screen object such as a table datasheet. I've encountered this error in an application that uses multiple instances of a form and needs to track whether the multiple form module is active or one of the other application module functions in which case all instances of the multiple forms (popups) need to have .visible set to false. I use the Screen.ActiveForm.Name call in the Form_Deactivate event.

    You can trap the error in the procedure's error handler and take action knowing the screen's active object is not a form.

    Example:
    
    Private Sub Form_Deactivate()
      On Error GoTo errHandler
      
      If Screen.ActiveForm.Name <> "AnApplicationForm" Then 'throws 2475 if not a form object
        sstatus = "status message"
      End If
    ExitSub:
      Exit Sub
    errHandler:
      If Err.Number = 2475 Then Resume Next 'screen active object is not a form i.e. datasheet
      MsgBox "Error in " & Me.Name & ".Form_Deactivate: " & Err.Number & " - " & Err.Description
      Resume ExitSub
    End Sub 'Form_Deactivate