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

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.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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"
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.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
PNeelyAuthor Commented:
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.
PNeelyAuthor Commented:
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".
Well done PNeely !

Glad that you solved!

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.