0

I have a userform with a textbox. I want to select the text within (highlight) after the keyDown event.

There are some restrictions to what the end user can enter. In case of wrong entry, a message would pop up, and the text in the textbox inviting the user to enter something should be selected.

I use the following code:

Me.txtbox_add_folder.SetFocus
Me.txtbox_add_folder.SelStart = 0
Me.txtbox_add_folder.SelLength = Len(Me.txtbox_add_folder.Text)

The setFocus doesn't work. It seems that it's because setFocus triggers a series of other events see this discussion.

The solution proposed in a different discussion is to include a DoCmd.CancelEvent or Cancel = True, so that the focus doesn't go to another control, but these commands don't work on Excel.

Community
  • 1
  • 1
GigaByte123
  • 175
  • 1
  • 2
  • 8
  • Please supply enough of your code so that we can try to replicate your issue. (I noticed in a comment to an answer you mention a `keyDown` event, but nothing in your question mentions that. So is this code in the `txtbox_add_folder_KeyDown` event? Or is it in the `txtbox_add_folder_Change` event? Or is it in the `txtbox_add_folder_Exit` event? Or where?) – YowE3K Nov 15 '17 at 18:35
  • TBH it sounds like you just want to use your code in the `Enter` event. That would seem to satisfy your "I have an excel UserForm with a textbox and I want to select the text within (highlight) so that the end user can see what to fill." requirement. (And, of course, you wouldn't need the `SetFocus` statement.) – YowE3K Nov 15 '17 at 18:44
  • I have added some details. In fact this code is within the `keyDown` event, and at the end of this event the focus is always lost and transferred to another control. – GigaByte123 Nov 16 '17 at 08:50
  • If you really don't want the user to ever be able to exit the textbox, just add `Cancel = True` in the `Exit` event. – YowE3K Nov 17 '17 at 06:01
  • A further solution to actually activate SetFocus might consist in 1.switching and reswitching the .Enabled property or alternatively 2.explicitly losing focus via any other control accepting focus (including also commandbuttons or frames) and eventually resetting focus again. See [Unexpected behaviour after using SetFocus on a text box](https://stackoverflow.com/questions/55011102/vba-userform-unexpected-behaviour-after-using-setfocus-on-a-textbox?noredirect=1&lq=1) – T.M. Oct 25 '19 at 18:07

4 Answers4

0
 Application.enableevents = false
 Me.txtbox_add_folder.SetFocus
 Me.txtbox_add_folder.SelStart = 0
 Me.txtbox_add_folder.SelLength = Len(Me.txtbox_add_folder.Text)
 Application.enableevents = true
Harassed Dad
  • 4,669
  • 1
  • 10
  • 12
  • Well I tried your solution but the focus was again lost and transferred to another control. It seems that the `keyDown` event ultimately transfers the focus to another control – GigaByte123 Nov 15 '17 at 17:29
0

This technique is a little hacky, but it has worked for me before. It puts the cursor in the textbox and performs a Ctrl+A command (select all), which would highlight the text in that textbox.

Me.txtbox_add_folder.SetFocus
SendKeys "^A"
Robby
  • 843
  • 3
  • 19
  • 53
0

I found a solution finally. Actually the focus was lost automatically after the event, so to avoid that, one should turn off the TabStop parameter in properties.

GigaByte123
  • 175
  • 1
  • 2
  • 8
0

My solution was to turn Tab Stop on ALL items that have Tab Stop except the textbox in question

Then, after the application does whatever I've asked it to do with the textbox entry, used Application.SendKeys "{TAB"}

I know it's crude, but when you want to use the textbox as a bar code scanner entry from across the room.

Worked for me, but it is a very simple user form.

Mike Sr
  • 511
  • 1
  • 5
  • 15