Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to put the cursor at the end of a text box 1

Status
Not open for further replies.

AllanWLJC

Technical User
Nov 21, 2006
5
US
I have several text boxes in a form. Some have text in them from an auto fill based on previous conditions. The tab key cycles through the boxes. As each box gets the focus, the text is selected. If data is entered at this point the original text is replaced. How can I program the form where if I tab to a box with text in it, the cursor goes to the end of the text and new data can be added with out deleting the old? I can use the left mouse button to move the cursor but I would like to do this automatically or from the keyboard.
 
Q110394 article
Add the following code to the Text1 GotFocus event:

Sub Text1_GotFocus ()
text1.SelStart = 0 ' Start selection at beginning.
text1.SelLength = Len(text1.Text) ' Length of text in Text1.
End Sub

------------------------- Extra informatin about properties

Syntax of SelLength, SelStart, and SelText Properties
The SelLength, SelStart, and SelText properties apply to combo boxes and text boxes, and behave as follows:

SelLength determines the number of characters selected.


SelStart determines the starting point of text selected. SelStart indicates the position of the insertion point if no text is currently selected.


SelText determines the string containing the currently selected text; consists of an empty string ("") if no characters are currently selected.


The SelLength, SelStart, SelText properties are not available at design time. They are only available at run time. They have the following syntax:


[form.]{combobox|textbox}.SelLength[ = length ]
[form.]{combobox|textbox}.SelStart[ = index ]
[form.]{combobox|textbox}.SelText[ = stringexpression ]

DougP, MCP, A+
 
the cursor goes to the end of the text
In the GotFocus event procedure of the control:
Me![name of control].SelStart = Len(Me![name of control].Value)

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
I'm confused!

AllanWLJC said:
How can I program the form where if I tab to a box with text in it, the cursor goes to the end of the text and new data can be added with out deleting the old?

DougP posted:
Code:
Sub Text1_GotFocus ()
      text1.SelStart = 0 
      text1.SelLength = Len(text1.Text)
End Sub

AllanWLJC wrote back:
That fixed the problem. Thanks

In point of fact, DougP's code does the same thing Allan's program was already doing, selecting all of the text box data, not moving the cursor to the end of the data. PHV's code does that!

Code:
Me![name of control].SelStart = Len(Me![name of control].Value)

The Missinglinq

Richmond, Virginia

There's ALWAYS more than one way to skin a cat!
 
It did select all of the data but using the select start command I was able to move the cursor to the end. Since the data was different lengths I arbitrarily moved the cursor 20 characters. This did the trick.

Private Sub Dedication_GotFocus()
Dedication.SelStart = 20
End Sub

Thanks for all the advice. Some day I may buy a good VB book so I do not have to ask for help on simple questions.
 
I arbitrarily moved the cursor 20 characters
No comment ...
Just to know, did you make sense with my suggestion ?
 
That also works. I am not sure which one is more efficient. Since there are only 3 possible pre-filled text “in memory of”, “in honor of” or blank, moving the cursor a set value should not be a problem. Your way is definitely better if the pre-filled data was of random or longer lengths. I am self taught in access and have only a meager foundation in visual basic so any help is appreciated.
 
I congratulate you on your restraint, PHV! [cheers]

The Missinglinq

Richmond, Virginia

There's ALWAYS more than one way to skin a cat!
 
I'll make the comment...
What if the text is 25 chars long? Lose last 5?
Sure, who needs them anyway!

Allan, that's a strong DON'T in a database and generally in programming business: restrict some values, field lengths and so on based on hard-coded numbers...

I'd go with PHV's solution if I were you...





[pipe]
Daniel Vlas
Systems Consultant

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top