Friday, November 19, 2021

 

Solution to Access error 2110 for setFocus

In Access forms, I often use a dummy text field called "txtFocusDummy" to which I set the focus when I want to de-select a field - which might be because I don't want the cursor to stay on the field (eg after selecting from a list) or because I want a quick way of committing an updated field value to the underlying table.

But I kept hitting error 2110, which said:

Run-time error '2110'

[Name of Application] can't move the focus to the control txtFocusDummy

and offered the choice of End and Debug.

If I chose Debug then, with the VBA Editor cisible, pressed F8 to continue, then all proceeded OK. So I knew it wasn't a problem with it being Enabled or Visible, or the right sort of field. So I reckoned that it had to be down to a timing issue - I was asking Access to move to a new field before it was ready to do so.

So, I replaced the setfocus instruction with a subroutine:

Public Sub fd()

    'moves focus to dummy field txtFocusDummy (among other things, ensures that field value is saved to underlying table)


tryAgain:

    On Error GoTo wait_a_while

    txtFocusDummy.SetFocus

    Exit Sub

wait_a_while:

    If True Then

        sSleep 10 '10 millseconds seems to be long enough!

        GoTo tryAgain

    Else

        If MsgBox("Sorry - Access needs a moment's rest to catch up. Please press OK to proceed") Then

            GoTo tryAgain

        End If

    End If

End Sub

And then (important step) in the VBA screen, I took Tools and Options, then the General tab and chose "Break on Unhandled Errors".

As you will see in the code, I have added the option to throw up a message box so that if I wish, I can see when the 2110 error is happening. But as set, the subroutine simply adds a 10 millisecond delay, which on my PC is enough to solve the problem.

The sSleep routine isn't mine, but here it is:

Private Declare Sub sapiSleep Lib "kernel32" _

        Alias "Sleep" _

        (ByVal dwMilliseconds As Long)


Public Sub sSleep(lngMilliSec As Long)

    If lngMilliSec > 0 Then

        Call sapiSleep(lngMilliSec)

    End If

End Sub

Hope this helps someone else (or that I find this myself when I hit the same problem again in a few years' time and have forgotten the answer).



Comments: Post a Comment



<< Home

This page is powered by Blogger. Isn't yours?