Trick behaviour when entering field

T

tkosel

I have a form for which I want all fields except one to have the text in
entire field selected when that field get the focus. I know that I can
effect the behaviour entering field using a Database wide setting under
tools, options, keyboard, behaviour entering field. Is there a way to to
this for just one field on a form?
 
K

Klatuu

In either case, use the GotFocus event of the control:

Select it all:

Me.MyTextBox.SelLength = Len(Me.MyTextBox)

Start at the beginning with nothing selected:

Me.MyTextBox.SelStart = 0
 
D

Dirk Goldgar

tkosel said:
I have a form for which I want all fields except one to have the text in
entire field selected when that field get the focus. I know that I can
effect the behaviour entering field using a Database wide setting under
tools, options, keyboard, behaviour entering field. Is there a way to to
this for just one field on a form?


You can use the control's GotFocus event to set its SelStart or SelLength
property -- I don't think it matters which you use. For example:

Private Sub ClientName_GotFocus()

Me!ClientName.SelStart = 0

End Sub

If you want to put the caret at the end of the text instead of the start,
you can use this instead:

With Me!ClientName
.SelStart = Len(.Text)
End With
 
M

Mark A. Sam

This will work when tabbing into the textbox, but not is you click on it
with your mouse. For that you can put the code into the KeyUp event.

Me.MyTextBox.SelStart = 0
Me.MyTextBox.SelLength = nz(Len(Me.MyTextBox),0)

Use the nz(function) to avoid Null errors.

Also it has been my experience that the term, Len(Me.MyTextBox) can be
problematic, so I also use the number 255 rather than the len() function.

Me.MyTextBox.SelLength = 255


God Bless,

Mark A. Sam
 
D

Dirk Goldgar

Mark A. Sam said:
This will work when tabbing into the textbox, but not is you click on it
with your mouse. For that you can put the code into the KeyUp event.

Me.MyTextBox.SelStart = 0
Me.MyTextBox.SelLength = nz(Len(Me.MyTextBox),0)

Use the nz(function) to avoid Null errors.

If you refer to the text box's Text property, you don't need the Nz
function, because the Text property (as distinct from the Value property)
can never be Null.
Also it has been my experience that the term, Len(Me.MyTextBox) can be
problematic, so I also use the number 255 rather than the len() function.

Me.MyTextBox.SelLength = 255

That won't work for a text box that is bound to a memo field, or unbound --
in either of those cases, the text box could be holding more than 255
characters of text.
 
M

Mark A. Sam

That won't work for a text box that is bound to a memo field, or
unbound -- in either of those cases, the text box could be holding more
than 255 characters of text.

I agree. I wasn't thinking of usage with a memo field. My experience is to
not select all of a memo field.
 
T

tkosel

To All,

Thanks, many of your suggestions were very usable and I have the issue
solved. I appreciate the tips very much.
 
L

Linq Adams via AccessMonster.com

SelLength and SelStart both take an Integer as an argument (which is limited
to 32,767) but a Memo field can hold twice that many characters (actually 4
times that, I understand, if the data is entered via code) so you have to be
careful using them with these fields! I can't imagine selecting an entire
memo field, but if you wanted to, say, place the cursor at the end of the
field, you should test it's length and if Len(YourMemoField) > 32767 set it
to 32767, which would at least get you a whole lot closer to the end of the
data.

If Len(YourMemoField) > 32767 Then
YourMemoField.SelStart = 32767
Else
YourMemoField.SelStart = Len(YourMemoField)
End If

--
There's ALWAYS more than one way to skin a cat!

Answers/posts based on Access 2000/2003

Message posted via AccessMonster.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top