I found that for a couple of hours this code worked:

If KeyCode = 37 Then

    Me.Text12.SetFocus

End If

Then it mysteriously stopped working. I thought it might have had something to do with processing the OnEnter subroutine (I move the cursor to the end of the text field).

However editing out the OnEnter subroutine didn't help. The only other thing I'd done was change my input/keyboard language (to be able to type quote marks and have them appear immediately, huzzah!)

I now have this code:

On Error Resume Next

If KeyCode = 37 Then

    Me.Text12.SetFocus
    Me.Text12.SetFocus

End If

Which works. So I know the 50+ pages of search results for SetFocus not working which relate to it being hidden, disabled, not visible, out for tea with the vicar etc do not apply here.

I've also tried setting the focus to another control first, and liberally applied DoEvents as well, neither of which helped.

Has anyone run into anything similar or is this just a really weird Heisenbug?

有帮助吗?

解决方案

You must have something else going on. Set a breakpoint on the first Me.Text12.SetFocus and watch what happens when you step through the code (F8).

I suspect that you have another process being triggered that's cancelling the first Me.Text12.SetFocus.

Another possibility is your DB as corrupted. This is something Access is prone to do, especially with lots of edits to the forms and/or reports. Go to File - Info, then click on Compact & Repair Database.
It's not a bad idea to have Access do this automatically whenever you close it. Go to File - Options - Current Database (tab), then check Compact on Close.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top