Insert text into textbox at cursor position

K

Keith

I would like to create a form with a textbox where the user can place the
cursor at any point in the textbox string and, by clicking a button, insert a
text string at that cursor location. For example, say the text box contains
the value "Keith". If the user places the cursor in the text box between the
"e" and the "i" and then clicks on a button which is coded to insert the
character string "89", then the value in the text box would be changed to
"Ke89ith". I guess what I need to know is how to get the cursor position in
the text box when I click the button.

Any help will be greatly appreciated.

Keith
 
C

Cindy

Keith -
You can store the position of the cursor inside a text box with the
field.selstart property (ie MyVariable = strFName.SelStart

I put that on the On Click property of the field where you want to do
the insert.

Then you could have code on your button that inserts the text
beginning with that position -
Using your example - name keith - cursor is between the e and i - we
want 89 to be inserted:

The variable would get set to 2 (I just put another textbox on the
form called unbWhere).

Dim sTemp As String
Dim sNew As String
Dim sFinal As String

Dim i As Integer
i = Me!unbWhere

sNew = "89" 'whatever code you had to get to this would go here
sTemp = Me!strFName
sFinal = Left(sTemp, i) & sNew & Mid(sTemp, i + 1)

Me!strFName = sFinal


(I also played around with mouse down and that didn't work - it
returns odd/differing answers in the unbWhere text box!!)

Cindy
 
M

Mr B

Keith,

In addition to knowing where the user clicks so you can add the characters
you want to add, you also will need to determine if after they clicked into
the filed, did they use the arror keys to move to another location or perhaps
did they even add some text of there own and then click the button that will
add the extra text.

Give this a try.

At the start of the module of your form, just below the Option Explicit
statement.
place this variable declaration:

Dim varStartPos As Long

Then, in the Key Down event of your text box, place the following code:

Select Case KeyCode
Case 39
If varStartPos < Len(Me.Text0) Then
varStartPos = varStartPos + 1
End If
Case 37
If varStartPos > 0 Then
varStartPos = varStartPos - 1
End If
End Select

This uses a Case statement to determine if the user used either the right
arrow or the left arrow to move the cursor to a different position.

Next, in the On Click Event of your text box, place this code:

varStartPos = Me.Text0.SelStart

Now, in the On Click event of your command button place this code:

Dim curPos
Dim strTmpVal As String
Dim strValIn As String
curPos = varStartPos
'insert "89" at cursor positon
strValIn = "89"
strTmpVal = Me.Text0

Me.Text0 = Left(strTmpVal, curPos) + strValIn + Right(strTmpVal,
Len(strTmpVal) - curPos)

Be careful about word wraping. The last line of my code is all on one line

In every place in all of the code above, replase the Text0 with the name of
your text box.

Now give it a test run. You should be able to click in a location and
immediately click your command button to add the extra text. Then try
clicking into the text box and then move the cursor with the right an left
arrow keys or adding some additional text. Then click your command button.
Your extra text should be added at the location where your cursor was when
you clicked the button.
--
HTH

Mr B
email if needed to:
draccess at askdoctoraccess dot com
 
K

Keith

Hi Cindy,

Thanks for the response. This works great if I click into the textbox when
it already have some text in it. I can't seem to get the appropriate
behavior if I start typing in the textbox when it's empty. The selstart
property doesn't seem to set properly when starting with an empty textbox,
even if I type some text and then click or cursor to another position. Do
you have any idea what I can do for these circumstances?

Thanks again for your help with this.

Sincerely,

Keith
 
M

Mr. B

Hi Cindy,

Thanks for the response. This works great if I click into the textbox when
it already have some text in it. I can't seem to get the appropriate
behavior if I start typing in the textbox when it's empty. The selstart
property doesn't seem to set properly when starting with an empty textbox,
even if I type some text and then click or cursor to another position. Do
you have any idea what I can do for these circumstances?

Thanks again for your help with this.

Sincerely,

Keith













- Show quoted text -

Keith,

You are quite right about what happens when the text box does not have
any entry.

Try changing the line:

strTmpVal = Me.Text0

in the code I posted, to:

If IsNull(Me.Text0) Then
strTmpVal = ""
Else
strTmpVal = Me.Text0
End If

Hope this helps.

Mr. B
 
K

Keith

Mr B,

Placing the following line of code in both the On Click and On Key Up events
did the trick for me:

CursorLocation = Me.TestText.SelStart

CursorLocation is a form module level variable.

Thanks for your input.

Sincerely,

Keith
 

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