Highlighting the contents of a textbox on entry. (2 Viewers)

k3v1np

New member
Local time
Today, 12:53
Joined
Mar 19, 2009
Messages
4
Looking for a VBA solution. I'm trying to have the contents of a textbox highlighted on entering it with the cursor so that it can be overwritten with the first keystroke. I have scoured a bunch of forums and googled the s_ _ t out of "highlight contents textbox" with zero success.
 

missinglinq

AWF VIP
Local time
Today, 15:53
Joined
Jun 20, 2003
Messages
6,423
If you want it to happen to all fields,

Goto Tools - Options - Keyboard and set "Behavior on Entering Field" to "Select Entire Field."

To just do it for a given field:
Code:
Private Sub FieldName_GotFocus()
  FieldName.SelStart = 0
  FieldName.SelLength = Len(Me.FieldName)
End Sub
 

k3v1np

New member
Local time
Today, 12:53
Joined
Mar 19, 2009
Messages
4
Thanks for your help.
(see attached file)
However, I must be doing something wrong cause I can't get it to work. I put the appropriate code in the on got focus event of the fname text box. When I click in that box it does not highlight the text. Also this is Access 2007 so i can't find the menu option to change the setting that you describe.
 

Attachments

  • database.zip
    26.6 KB · Views: 607

k3v1np

New member
Local time
Today, 12:53
Joined
Mar 19, 2009
Messages
4
I found the same examples that missinglinq posted above on a few other forums and I can't understand why I am unable to get it to work like i think it should. Should it not highlight all the text in the field when you click in it with the mouse coursor? Cause mine just won't do that. Something so simple shouldn't be so difficult. Or is it me who is simple?
 

evanscamman

Registered User.
Local time
Today, 12:53
Joined
Feb 25, 2007
Messages
274
The code is good code. If you try to enter the control by tabbing into it you will see that it does select the text.

The problem with clicking into the control is that the click event overwrites your selection.

This may not be the cleanest work-a-round, but you could use the timer event to get around this:

Private Sub FieldName_GotFocus()
Me.TimerInterval = 10
End Sub

Private Sub Form_Timer
FieldName.SelStart = 0
FieldName.SelLength = Len(Me.FieldName)
End Sub


This delays the Select code by 10 milliseconds and gives the mouse event time to have already fired.

Evan
 

missinglinq

AWF VIP
Local time
Today, 15:53
Joined
Jun 20, 2003
Messages
6,423
I haven't had anyone have trouble with this for ages, but I seem to remember from way back in my addled mind that occasionally you also need to place the code in the textbox OnClick event. Note that that's in addition to the GotFocus event.

Don't run 2007, so I can't tell where you have to for Options.

Just out of curiosity, do you have any VBA code that runs? In version 2007, no VBA code will execute unless that the folder holding the database has been designated as a “trusted” location.

To trust your folder, click:
* Office Button (top left)
* Access Options (bottom of dialog)
* Trust Center (left)
* Trust Center Settings (button)
* Trusted Locations (left)
* Add new location (button)
 

k3v1np

New member
Local time
Today, 12:53
Joined
Mar 19, 2009
Messages
4
aaaah YES. Placing it the same code in the on click property solves the problem. Thank you, thank you and thank you.
 

ndfan77

New member
Local time
Today, 15:53
Joined
Jan 26, 2015
Messages
1
I realize it is now six years later but, I ran into a need for this too -- and wanted to add that if you want users to retain the ability to also select existing text with the mouse, the TextBox Click event needs to be a little smarter:

Code:
Private Sub MyTextBox_Click()
    [COLOR="Red"]If MyTextBox.SelLength = 0 Then[/COLOR]     'Don't change selection if user has already selected text
        MyTextBox.SelStart = 0
	MyTextBox.SelLength = Len(MyTextBox.Value & "")
    [COLOR="red"]End If[/COLOR]
End Sub
 

atrium

Registered User.
Local time
Tomorrow, 06:53
Joined
May 13, 2014
Messages
348
Missinglinq

The last bit of code to highlight the text worked wonders.

Thank you

Atrium
 

Gavx

Registered User.
Local time
Tomorrow, 06:53
Joined
Mar 8, 2014
Messages
151
I tried to implement this on a text box that is formatted for Currency but the code only selects the numbers to the left of the decimal point.
Can anyone please advise the fix?

thanks
gavin
 

missinglinq

AWF VIP
Local time
Today, 15:53
Joined
Jun 20, 2003
Messages
6,423
I tried to implement this on a text box that is formatted for Currency but the code only selects the numbers to the left of the decimal point.
Can anyone please advise the fix?
Never run into this before but you are indeed correct! Only workaround I could come up with (it's been a very long day and I going to La La Land soon) is to assign the field as Text then use this

Code:
txtCurrency.SelStart = 0
txtCurrency.SelLength = Left(txtCurrency, InStr(txtCurrency, ".")) & Mid(InStr(TxtCurrency, ","), 3)
You'd have to deal with using one of the Conversion Functions when dealing with the Field if you're going to be doing math with it...same thing, in reverse, if you're using math to derive the amount.

Hope this helps!

Linq ;0)>
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 15:53
Joined
Feb 19, 2002
Messages
42,973
In my databases, this is the default behavior. Check the properties of the database. Perhaps yours are different.
 

Attachments

  • SelectEntireField.JPG
    SelectEntireField.JPG
    59.5 KB · Views: 1,363

isladogs

MVP / VIP
Local time
Today, 19:53
Joined
Jan 14, 2017
Messages
18,186
If your app is to be run by several users/ on several workstations, do bear in mind that the settings in Pat's screenshot apply to the client rather than the current database.
In other words you can't rely on that setting being true for other users
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 15:53
Joined
Feb 19, 2002
Messages
42,973
I'm working on getting that changed. Every year when I send my Christmas wish list to the MS Access team, a way to control properties for THIS database specifically and for new databases created by me in general is near the top of the list.

Keep in mind that what I posted is the default setting. If the app is distributed to people who do not themselves create Access databases, the settings would not have been changed. However, if the app will be used by users who do get their hands dirty with Access, then Colin is correct.
 

Users who are viewing this thread

Top Bottom