Solved

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

Posted on 2007-11-15
7
196 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 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
Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

Suggested Solutions

Introduction When developing Access applications, often we need to know whether an object exists.  This article presents a quick and reliable routine to determine if an object exists without that object being opened. If you wanted to inspect/ite…
Regardless of which version on MS Access you are using, one of the harder data-entry forms to create is one where most data from previous entries needs to be appended to new records, especially when there are numerous fields and records involved.  W…
Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…

829 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