Hack #20. Help Users Enter Additional Text

Place the insertion point at the end of the text in a text box so that additional entries land just where they should.

This technique makes so much sense, and yet it is often overlooked. Have you ever noticed that when you're editing data in a form, and you tab into a text box, the entire text is selected? Unfortunately, this default behavior makes the data vulnerable to accidental overwriting. Figure 3-6 shows the address text box fully selected. Assuming an edit is needed to add additional text (not to replace the text), the user must move his mouse to the end of the text and then click to deselect it.

Automatically selected data, vulnerable to an accidental delete or overwrite

Figure 3-6. Automatically selected data, vulnerable to an accidental delete or overwrite

Wouldn't it be nice if the user didn't have to click first to deselect the text? Of course, there is a way to do this. It takes just a smattering of code.

Many controls, including text boxes, have an Enter event, which is triggered when the control is clicked or tabbed into. This is the event in which you can place code to move the cursor to the end of the text, before the user has a chance to enter any keystrokes. The following code snippet is based on the control being named CompanyAddress1:

	Private Sub CompanyAddress1_Enter()
      Dim text_length As Integer
	  text_length = Len(Me.CompanyAddress1)
	  Me.CompanyAddress1.SelStart = text_length
	End Sub

The length ...

Get Access Hacks now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.