Problems with using SelStart to insert text into a TextBox

Posted on 2004-11-29
Last Modified: 2008-02-01
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
    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

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.

Question by:WorldsTallestTree
    LVL 54

    Expert Comment

    Just a thought:

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

    And/or tested with the LostFocus event.


    Author Comment

    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)
    LVL 54

    Accepted Solution

    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.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
    Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
    Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…
    With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

    758 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now