Solved

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

Posted on 2007-11-15
7
192 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
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 119

Expert Comment

by:Rey Obrero
Comment Utility
try

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

Author Comment

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

Expert Comment

by:Rick_Rickards
Comment Utility
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
Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

 
LVL 5

Expert Comment

by:pavaneeshkumar
Comment Utility
  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
Comment Utility
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
Comment Utility
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 119

Assisted Solution

by:Rey Obrero
Rey Obrero earned 100 total points
Comment Utility
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

This article is a continuation or rather an extension from Cascading Combos (http://www.experts-exchange.com/A_5949.html) and builds on examples developed in detail there. It should be understandable alone, but I recommend reading the previous artic…
The first two articles in this short series — Using a Criteria Form to Filter Records (http://www.experts-exchange.com/A_6069.html) and Building a Custom Filter (http://www.experts-exchange.com/A_6070.html) — discuss in some detail how a form can be…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

772 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now