How to: Prevent the Accidental Erasure of Data When Moving Between Controls on a Form

Access Developer Reference

When you tab from one text box or memo field to another in a form, the text in the control is highlighted. This makes it easy for users to accidentally delete the text by pressing a key. By using a few lines of code, you can move the insertion point to the first position in the text box, minimizing the risk of accidentally deleting the text.

To do this, create a procedure for the text box's GotFocus event. In the GotFocus event procedure, set the SelLength property of the text box to its SelStart property. The following example illustrates how to do this for a text box named txtFirstName.

  Private Sub txtFirstName_GotFocus()
Me.txtFirstName.SelLength = Me.txtFirstName.SelStart

End Sub