move cursor to end of text in textbox after setfocus (1 Viewer)

fire2ice

Expert Novice
Local time
Today, 14:31
Joined
Feb 21, 2008
Messages
80
I'm trying to figure out how to move the cursor to end of text in a textbox after I set focus to the control. Right now all of the text is highlighted when the focus is set. The code I'm working with is very simple:

-----------------------------------------------------------------------------
Private Sub cmdEditComments_Click()
Me.Comments.Locked = False
Me.Comments.SetFocus
End Sub
-----------------------------------------------------------------------------

I'm thinking I need to do something with Chr(3), but am not sure of syntax.

Thanks!
 
Last edited:

DCrake

Remembered
Local time
Today, 19:31
Joined
Jun 8, 2005
Messages
8,626
Simple Software Solutions

Go to Tools - Options - Keyboard and set the Keyboard behaviour to End of Field.
 

fire2ice

Expert Novice
Local time
Today, 14:31
Joined
Feb 21, 2008
Messages
80
Thank you for the reply. However, I don't want to implement that change company-wide. I'm looking for a VB solution that will be universally applied no matter the individual settings.
 

datAdrenaline

AWF VIP
Local time
Today, 13:31
Joined
Jun 23, 2008
Messages
697
What you can do is utilize the .SelStart and .SelLength properties of a Text Box or Combo Box control.

Here is a block of code that I have in a Standard Module ...

Code:
Public Function pfPositionCursor(ctl As Control, lngWhere As Long)
 
    Select Case ctl.ControlType
        Case AcControlType.acTextBox, AcControlType.acComboBox
 
            ctl.SelStart = lngWhere
            ctl.SelLength = 0
 
        Case Else
            'Do Nothing
    End Select
 
End Function

Then in the OnEnter Event PROPERTY of the form control you wish to control the initial cursor position just enter the function call like this:

If you want the beginning of the text ...
=pfPositionCursor([NameOfTheControl],0)

If you want the end of the text ...
=pfPositionCursor([NameOfTheControl],Len([NameOfTheControl] & ""))

...

With this code implemented, when you tab into a control that has this function call, your blinking cursor will be where ever you specify. Plus, you only have one block of code to manage! ... If you need to utilize the OnEvent event for other VBA code, just be sure to call the function in the beginning of the event procedure code block, then your cursor will be where your want it.
 

fire2ice

Expert Novice
Local time
Today, 14:31
Joined
Feb 21, 2008
Messages
80
If I want to do this from a command button, should it work the same?

Doing the following code produces an error stating "Compile error: Expected: line number or label or statement or end statement" in the VB editor:

----------------------------------------------------------------------------
Private Sub cmdEditResponse_Click()

Me.Response.Locked = False
Me.Response.SetFocus
=pfPositionCursor([Response],Len([Response] & ""))

End Sub
----------------------------------------------------------------------------

Thanks for the input!
 

datAdrenaline

AWF VIP
Local time
Today, 13:31
Joined
Jun 23, 2008
Messages
697
Hey Fire2Ice ....

The syntax I gave you was if you are calling the function from the event PROPERTY (the place where you normally see "[Event Procedure]") ... in VBA, the call would look like like this ...

Code:
Private Sub cmdEditResponse_Click()
    
    Me.Response.Locked = False
    Me.Response.SetFocus
    pfPositionCursor Me.Response, Len(Me.Response & "")
    
End Sub
 

fire2ice

Expert Novice
Local time
Today, 14:31
Joined
Feb 21, 2008
Messages
80
Thanks much. That did the trick! Now I'll have to wrap my head around the syntax so that I know what (more specifically... how) to do this in the future.
 

datAdrenaline

AWF VIP
Local time
Today, 13:31
Joined
Jun 23, 2008
Messages
697
You are most welcome!! ... As you study the syntax, please don't hesitate to ask questions!! ...
 

liamfitz

Registered User.
Local time
Today, 19:31
Joined
May 17, 2012
Messages
240
This looks like, just what I'm after, but what I've tried ( adapted for my control name ) is not working. Here's my code. Any help appreciated.
Code:
.Text = Left(strComments, l) & ")"
            pfPositionCursor Me.txtOutcomes, Len(Me.txtOutcomes & "")
 

liamfitz

Registered User.
Local time
Today, 19:31
Joined
May 17, 2012
Messages
240
Apologies, I didn't copy in the function ... der, sorry.
 

radek225

Registered User.
Local time
Today, 11:31
Joined
Apr 4, 2013
Messages
307
How to set pfPositionCursor exactly in second line with this code?
 

monbois

New member
Local time
Today, 14:31
Joined
May 31, 2012
Messages
7
I tried this code, but Access 2010 doesn't know what to make of this name: pfPositionCursor

Doesn't work for me at all. :(
 

datAdrenaline

AWF VIP
Local time
Today, 13:31
Joined
Jun 23, 2008
Messages
697
pfPositionCursor is a User Defined Function that should be placed in a Standard Module that does not share a name with any procedure.

The code for pfPositionCursor() is found post #4 of this thread.
 

monbois

New member
Local time
Today, 14:31
Joined
May 31, 2012
Messages
7
Found an exceedingly simple solution on the UK website DatabaseDev:

' place cursor at end of text in Text Box
' ----------------------------------------------------------------
Me!TextBox.SelStart = Me!TextBox.SelLength

I put this code in the Key Up event; after each keystroke I want a list box to update by continuing to filter the contents after each character is entered. To do this, I have to exit the text field and focus on another field in order to capture the value just entered, then re-focus on the text field and run the code above to send the cursor to the end of the line of text. Works perfectly!
 

ronymaxwell

Registered User.
Local time
Today, 11:31
Joined
Apr 27, 2012
Messages
18
monbois, thanks for your solution. I have a list box with a number of strings in it, clicking on one causes it to appear in a text box. This is as a short cut for the user whn typing into the text box. I needed the user to be able to continue typing without interruption. Your simple piece of code works perfectly.
 

Users who are viewing this thread

Top Bottom