VBA: (1) Select all text in textbox with focus; (2) replace selected text when user starts typing

Posted on 2004-11-18
Medium Priority
Last Modified: 2010-05-18
Can't seem to find an answer to this one in the PAQs... When a user clicks in a text box, all text should be highlighted. When they start typing, the highlighted text should be replaced.

I am able to highlight the text using the SelStart and SelLength properties, but when I start typing, Access adds to the exisitng text rather than replacing it.

Grateful for any help.
Question by:PNeely
  • 2
  • 2

Expert Comment

ID: 12612899
U can use the action On_Keydown to catch the instance of typing.
The variable KeyCode contains the key pressed.

To process the action yourself you can use the seltext etc. u mentioned.
To prohibit Access from adding the text, you can set Keycode to empty

add "keycode = empty"

Then MS Access stops processing the keyboardinput.....

Does this help ??

--- Greetz Norbert ---
"I'm realist, I live in an illusion"

Accepted Solution

arcross earned 200 total points
ID: 12613107
Hi Pneely,
When you press tab or enter keys the focus moves to the next control and highlights all as normal. If you want to highligt when you click in a text box, put this on click event of your textbox.

Me.mytextbox.selstart = 0
me.mytextbox.sellength = len(me.mytextbox)

Dont knok if you have this already but this works fine. Selects everything when user clicks in textbox and when he/she types it replaces the text.


Author Comment

ID: 12613367
Thanks, both of you.

Alvaro, yours is what I was looking for. You're right, I had tried this before, but I had put it in the wrong event. I actually have a combo box, not a text box. To get it to work with the combo box, I had to put the following code under the MouseUp event:

With Me.cboControl
    .SelStart = 0
    .SelLength = Len(.Text)
End With

If I put it under the the Click or Enter events, the selection would not show since other events fire subsequently to these which cancel the selection. If I put it under the MouseUp event, I experienced the problem I wrote about in this post: the text would select, but the cursor would be wherever the mouse was pointing in the text when the user clicked the combo box. To select the text and have the cursor at the beginning, it seems I can only put this code in the MouseUp event -- for a combo box, anyway.

Anyway, I write all this only in case it's helpful to someone reading this later. The points are your Alvaro.

Many thanks.

Author Comment

ID: 12613381
Whoops, correction to the above phrase: "If I put it under the MouseUp event, I experienced the problem I wrote about in this post".

It should read, "If I put it under the MouseDown event, I experienced the problem I wrote about in this post".

Expert Comment

ID: 12613444
Well done PNeely !

Glad that you solved!


Featured Post

Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Access custom database properties are useful for storing miscellaneous bits of information in a format that persists through database closing and reopening.  This article shows how to create and use them.
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question