Link to home
Start Free TrialLog in
Avatar of PNeely
PNeelyFlag for United Kingdom of Great Britain and Northern Ireland

asked on

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.
Avatar of NikoFraight
NikoFraight

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"
ASKER CERTIFIED SOLUTION
Avatar of arcross
arcross
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of PNeely

ASKER

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.
Avatar of PNeely

ASKER

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!

Álvaro