Solved

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

Posted on 2007-11-15
7
197 Views
Last Modified: 2013-11-28
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
Comment
Question by:donhannam
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 20296135
try

    me.text0.selstart = 0
    me.text0.selLength = len(me.text0.Text)
0
 

Author Comment

by:donhannam
ID: 20296164
Thanks - I tried sellength but still leaves cursor at start of text.
0
 
LVL 16

Expert Comment

by:Rick_Rickards
ID: 20296273
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 5

Expert Comment

by:pavaneeshkumar
ID: 20296322
  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
 

Author Comment

by:donhannam
ID: 20296422
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
 
LVL 5

Accepted Solution

by:
pavaneeshkumar earned 150 total points
ID: 20296614
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
 
LVL 120

Assisted Solution

by:Rey Obrero (Capricorn1)
Rey Obrero (Capricorn1) earned 100 total points
ID: 20297731
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

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
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.

762 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