?
Solved

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

Posted on 2007-11-15
7
Medium Priority
?
200 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
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
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 450 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 300 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

It’s the first day of March, the weather is starting to warm up and the excitement of the upcoming St. Patrick’s Day holiday can be felt throughout the world.
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…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

800 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