Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 204
  • Last Modified:

I would like to set focus to a field in edit mode - text in field highlighted

I have an after update event on a text field - if entered incorrectly I would like to return to this field and allow user to change - I can use setfocus to get back to this field but cursor is sitting at start of text - I would like to return so that is highlighting all text the way it does when you tab into a new field.

At present just use text.setfocus - have tried setting focus to another field first then one I want but no joy
0
donhannam
Asked:
donhannam
  • 2
  • 2
  • 2
  • +1
2 Solutions
 
Rey Obrero (Capricorn1)Commented:
try

    me.text0.selstart = 0
    me.text0.selLength = len(me.text0.Text)
0
 
donhannamAuthor Commented:
Thanks - I tried sellength but still leaves cursor at start of text.
0
 
Rick_RickardsCommented:
By setting the property named SelLength to be equal to the length of the string currently present you will effectively select all the text that is in the text box upon entering it from the OnEnter event as follows...

Private Sub txt1_Enter()
    Me.txt1.SelLength = Len(Me.txt1.Value & "")
End Sub

As a bonus you may also be pleasantly surprised to find that this approach will display an input mask even if there isn't any text in the field since the input mask (if there is one) appears once SelLength has a value even if that value is 0.  Also note the concatenation of an empty string & "".   This is done so that if the value is Null it will convert it to an empty string and thus Len() will not error whereas it would if Len() were to try to return the length of Null.
0
Get 10% Off Your First Squarespace Website

Ready to showcase your work, publish content or promote your business online? With Squarespace’s award-winning templates and 24/7 customer service, getting started is simple. Head to Squarespace.com and use offer code ‘EXPERTS’ to get 10% off your first purchase.

 
pavaneeshkumarCommented:
  Replace your code with this
 
    Text1.SetFocus
    Text1.SelStart = 0
    Text1.SelLength = Len(    Text1.Text)

I checked it is working.

If you can provide the event code for validation & returining focus
0
 
donhannamAuthor Commented:
Tried combinations so far and cannot get to do anything different.

Using access 2003.

I have set up a new form with 2 text boxes - text1 and text3 with following code

Private Sub Text1_AfterUpdate()  
    Me.Text3.SetFocus
    Me.Text1.SetFocus
    Me.Text1.SelStart = 0
    Me.Text1.SelLength = Len(Me.Text1.Text)    
End Sub

I have also tried
Me.Text1.SelLength = Len(Me.Text1.Value & "")

In either case I type text in field and enter - cursor returns to Text1 box OK but with cursor at start of text entered.

Would like it to highlight all text so if I start typing will replace text
0
 
pavaneeshkumarCommented:
I checked this one this code will not going to work in
           After_Updated Event
it will work fine in
           LostFoucs event

Mean while i will try to get some more details why it doesn't works in After_Updated Event
0
 
Rey Obrero (Capricorn1)Commented:
donhannam,
you mentioned < if entered incorrectly I would like to return to this field and allow user to change> how do you determine that the entry is incorrect?

you can use the lostfocus or exit event

private sub text1_exit(cancel as integer)
if len(me.text1)< 10 then
  cancel=true
  Me.Text1.SelStart = 0
  Me.Text1.SelLength = Len(Me.Text1.Text)    
end sub
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 2
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now