Solved Highlight field contents on click (1 Viewer)

estelleh

Member
Local time
Today, 03:02
Joined
Jul 16, 2021
Messages
56
I have hit a bit of a snag with moving between fields on a form. If I use the <TAB> key the contents of the field is highlighted (as I expect it to be - I selected "Behavior on entering field / Select entire field" in Options / Client Settings. However, if I click on a field, the cursor is placed wherever I click, instead of highlighting the contents of the field. I have plaued with putting this in the On_click event of a field:

Code:
FieldName.SelStart = 0
FieldName.SelLength = Len(Me.FieldName)

It works fine on an ordinary textbox, but on a currency field it selects the "$" and the first number only. Also, it seems like a Herculean task to put code in each textbox's On_Click event throughout the whole system.

Is there an easier way to make Access 2016 highlight the contents of a field when clicking on it?
 

CJ_London

Super Moderator
Staff member
Local time
Today, 02:02
Joined
Feb 19, 2013
Messages
16,610
use conditional formatting. no code required

select all the controls you want to have the effect, then in the ribbon click on conditional formatting>new rule and select 'field has focus'. Then select whatever background colour you want
 

oleronesoftwares

Passionate Learner
Local time
Yesterday, 18:02
Joined
Sep 22, 2014
Messages
1,159
hi, write the below vba code, under the on click event of the control

Me.TextBoxName.SelStart = 0
Me.TextBoxName.SelLength = Nz(Len(Me.TextBoxName), 0)




where TextBoxName is name of the control in the form
 

June7

AWF VIP
Local time
Yesterday, 17:02
Joined
Mar 9, 2014
Messages
5,466
Do you want to highlight control background or do you want to select contents?

Afraid there is no simple/quick way to implement the selection code.
 

theDBguy

I’m here to help
Staff member
Local time
Yesterday, 18:02
Joined
Oct 29, 2018
Messages
21,454
Hi. Not in front of a computer right now, but I think there is a setting in the Options screen to select the entire content when you enter each control.
 

June7

AWF VIP
Local time
Yesterday, 17:02
Joined
Mar 9, 2014
Messages
5,466
Hi. Not in front of a computer right now, but I think there is a setting in the Options screen to select the entire content when you enter each control.
Read OP, says that was done. Works for Tab or Enter control navigation but not mouse click.
 

estelleh

Member
Local time
Today, 03:02
Joined
Jul 16, 2021
Messages
56
Do you want to highlight control background or do you want to select contents?

Afraid there is no simple/quick way to implement the selection code.
Damn - I need to select the content.....
 

estelleh

Member
Local time
Today, 03:02
Joined
Jul 16, 2021
Messages
56
hi, write the below vba code, under the on click event of the control

Me.TextBoxName.SelStart = 0
Me.TextBoxName.SelLength = Nz(Len(Me.TextBoxName), 0)




where TextBoxName is name of the control in the form
Have that - per my OP - it works for a straight textbox, but for a currency textbox it only selects the currency symbol and the first number.

Also, I have 36 forms, some of them with a LOT of controls.... I could be here for a month of Sundays programming each On_Click event.
 

estelleh

Member
Local time
Today, 03:02
Joined
Jul 16, 2021
Messages
56
use conditional formatting. no code required

select all the controls you want to have the effect, then in the ribbon click on conditional formatting>new rule and select 'field has focus'. Then select whatever background colour you want
Thanks - am after selecting the contents of the textbox though, not changing the background colour....
 

CJ_London

Super Moderator
Staff member
Local time
Today, 02:02
Joined
Feb 19, 2013
Messages
16,610
I could be here for a month of Sundays programming each On_Click event.
create a public function in a standard module - something like

Code:
Function beenClicked()

    With Screen.ActiveControl
        .SelStart = 0
        Select Case .Parent.Recordset.Fields(.ControlSource).Type
            Case 5 'currency
                    .SelLength = Len(Format(Nz(.Value, 0), "£#,##0.00"))'change this to the currency format you require
            Case Else
                     .SelLength = Len(Nz(.Value, ""))
        End Select
     
    End With

end function

then open a form in design view, select all the controls you want to have this functionality and against the onclick event (i.e. where you would normally see [Event Procedure]) put

=beenClicked()

repeat for other forms.

If you have issues with other field types - here is a link to their enum identifier, extend the case statement for other field types

Note this does not work for unbound controls, but you could modify it to check the controlsource and take appropriate action. I have not included error handling since I've no idea what errors you could get
 

theDBguy

I’m here to help
Staff member
Local time
Yesterday, 18:02
Joined
Oct 29, 2018
Messages
21,454
Read OP, says that was done. Works for Tab or Enter control navigation but not mouse click.
Oops, sorry I missed that. It was still too early here when I checked the forum. Cheers!
 

June7

AWF VIP
Local time
Yesterday, 17:02
Joined
Mar 9, 2014
Messages
5,466
I stand corrected. I forget about "ActiveControl", never used it.
 

CJ_London

Super Moderator
Staff member
Local time
Today, 02:02
Joined
Feb 19, 2013
Messages
16,610
So did you solve your problem? Or try my suggestion in post#12?
 

estelleh

Member
Local time
Today, 03:02
Joined
Jul 16, 2021
Messages
56
So did you solve your problem? Or try my suggestion in post#12?
I solved the problem by explaining to the client why what she wanted is a bad idea!

In playing around with solutions (including yours to a degree - thanks!) I realised that selecting the contents of a textbox in the On_Click event would mean the client would never be able to edit the contents of the textbox, but would always have to retype the entire field every time they clicked on any object - unlike the selection by using the TAB key, which still allows one to position the cursor at a specific spot.

As a matter of interest, I came up with a work around for the currency fields - running the code in debug, it turned out that the code

Code:
FieldName.SelStart = 0
FieldName.SelLength = Len(Me.FieldName)

does in fact return the correct number of characters (6), but the character selection is wrong because of the currency symbol and the formatting of the amount with a thousands seperator. So the value R 1 234.56 correctly returns a value of 6 characters, but only selects R 1 23 (being the first 6 characters). I fixed this by adding an arbitrary amount (in my test situation I made it 5 - although it didn't seem to matter what number I used) which then selected the entire contents.

Thanks everyone!
 

June7

AWF VIP
Local time
Yesterday, 17:02
Joined
Mar 9, 2014
Messages
5,466
Use OnEnter or GotFocus event instead of OnClick. Then user can type or click or use cursor movement keys.
 

Users who are viewing this thread

Top Bottom