Problems with using SelStart to insert text into a TextBox

I am trying to use a TextBox in an Access form to allow users to set up a template for a report.  The users
need to be able to type in text freely into the text box, but also to be able to insert a string from another combo box
on the form (which basically contains a list of fields from a table in the database) by clicking on a button.  I want
the inserted string to be inserted at the current cursor position in the TextBox.  If I use the following code


Dim lngCashCursorPos as long

Private Sub btnCashFieldInsert_Click()
    Dim strCashLeft As String
    Dim strCashRight As String
    Dim lngCashTextLength As Long
   
    DoEvents    
   
    txtCash.SetFocus
    If (Not IsNull(txtCash.Text)) Then
        lngCashTextLength = VBA.Len(txtCash.Text)
        strLeft = VBA.Left(txtCash.Text, lngCashCursorPos)
        strRight = VBA.Right(txtCash.Text, lngCashTextLength - lngCashCursorPos)
        txtCash.Text = strLeft & "example inserted text" & strRight
    End If
   
End Sub

Private Sub txtCash_Exit(Cancel As Integer)
    lngCashCursorPos = txtCash.SelStart
End Sub


then if the user clicks at the appropriate place in the TextBox and then clicks on the insert button, the string "example inserted text"
is inserted fine.  But if the user clicks in the TextBox and then types in a few more characters and then presses the insert button, then
the inserted text is inserted at the beginning of the TextBox.  This seems to be because once the TextBox has been edited, the value
of SelStart is 0 when the Exit event is called.  This is fixed by using the following Change event handler and removing the Exit handler
above

Private Sub txtCash_Change()
    lngCashCursorPos = txtCash.SelStart
End Sub

But then if the user clicks in the TextBox without editing anything then the inserted text is inserted at the beginning.  If I try putting in
both the Change and Exit event handlers above then the Exit event handler just overrides the Change event handler.  How can I handle
both of these situations?  I have tried using txtCash.value instead of txtCash.Text in the above without success.


 
WorldsTallestTreeAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

nico5038Commented:
Just a thought:

Private Sub txtCash_Exit(Cancel As Integer)
    if txtCash.SelStart > 0 then
       lngCashCursorPos = txtCash.SelStart
    endif
End Sub

And/or tested with the LostFocus event.

Nic;o)
0
WorldsTallestTreeAuthor Commented:
Nic;o)... that certainly improves the situation, but doesn't cover the situation when the user clicks at the
beginning of the text box and then tries to insert something.

I have gotten rid of both the onChange and onExit event handlers and replaced them with

Private Sub txtCash_KeyDown(KeyCode As Integer, Shift As Integer)
    lngCashCursorPos = txtCash.SelStart
End Sub

Private Sub txtCash_Click()
    lngCashCursorPos = txtCash.SelStart
End Sub

This seems to work OK.
I would gladly give the points for this question for anyone who can give me a link to some documentation that
describes in detail under what circumstances all of the various VB control events are fired, and in what order
and how the various properties of controls respond to them (for example i don't understand when and why
TextBox.SelStart changes, or when TextBox.Text becomes TextBox.Value etc)
0
nico5038Commented:
Hmm, so I guess the combination of your new Click event combined with "my" Exit should work too.
I'm always a bit reluctant to use the Key events as they sometimes slowdown processing.

I got my event-sequence information in the past from the Access Programmer guide that comes with the Developer edition, but now you should be able to get it from the help (F1) file or the Microsoft site. (E.g. http://msdn.microsoft.com/library/default.asp?url=/library/en-us/bapp2000/html/acbachap05.asp)

Nic;o)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.