Share via

Set cursor focus on specific text box

Anonymous
2011-12-20T19:08:19+00:00

In Access VBA, after clearing a text box of a non-valid entry, how do I position the cursor at the same text box, after displaying a MsgBox error notice, instead of the cursor's automatically moving to the next control in the tab order?  I tried using Me!ControlName.SetFocus in the AfterUpdate event, but to no avail.

Microsoft 365 and Office | Access | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Anonymous
2011-12-21T17:28:26+00:00

A response to another thread resolved my problem.  The answer was to, in the AfterUpdate event, first set focus on some other control, then to set focus on the subject text box control - which accomplishes my aim: display an error msg, clear the entry in the AfterUpdate event, and set focus on the empty text box for a corrected entry.

I think it would be simpler just to undo the invalid entry in the text box's BeforeUpdate event, while also cancelling the event.  Like this (extending HansV's code):

Private Sub txtSomething_BeforeUpdate(Cancel As Integer)

    If <your condition here> Then

        MsgBox "Incorrect entry", vbExclamation

        Cancel = True

        Me.txtSomething.Undo

    End If

End Sub

Was this answer helpful?

0 comments No comments

3 additional answers

Sort by: Most helpful
  1. Anonymous
    2011-12-21T16:18:51+00:00

    A response to another thread resolved my problem.  The answer was to, in the AfterUpdate event, first set focus on some other control, then to set focus on the subject text box control - which accomplishes my aim: display an error msg, clear the entry in the AfterUpdate event, and set focus on the empty text box for a corrected entry.

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2011-12-21T16:00:26+00:00

    Using the "Cancel=True" does retain focus on the text box, however it prevents the AfterUpdate event from occurring in which I am able to clear the erroneous entry.  Consequently, the erroneous entry remains displayed in the text box.

    My preference is to be able to notify the user of the error, clear the textbox in the AfterUpdate event (or by some other means), and then set the focus (cursor) on the textbox with a null or 0-length string.

    Was this answer helpful?

    0 comments No comments
  3. HansV 462.6K Reputation points
    2011-12-20T20:04:56+00:00

    Use the Before Update event of the text box and set Cancel = True if the entry is invalid. For example:

    Private Sub txtSomething_BeforeUpdate(Cancel As Integer)

        If <your condition here> Then

            MsgBox "Incorrect entry", vbExclamation

            Cancel = True

        End If

    End Sub

    Was this answer helpful?

    0 comments No comments