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

Posted on 2004-11-18
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
    LVL 1

    Expert Comment

    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"
    LVL 8

    Accepted Solution

    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

    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

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

    Expert Comment

    Well done PNeely !

    Glad that you solved!


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
    A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
    In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
    In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

    779 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

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now