[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Problems with using SelStart to insert text into a TextBox

Posted on 2004-11-29
3
Medium Priority
?
651 Views
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
   
    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.


 
0
Comment
Question by:WorldsTallestTree
  • 2
3 Comments
 
LVL 54

Expert Comment

by:nico5038
ID: 12712431
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
 

Author Comment

by:WorldsTallestTree
ID: 12713161
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
 
LVL 54

Accepted Solution

by:
nico5038 earned 2000 total points
ID: 12713528
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

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Windows Phone Theme Colours is a tight, powerful, and well balanced palette. This tiny Access application makes it a snap to select and pick a value. And it doubles as an intro to implementing WithEvents, one of Access' hidden gems.
In a use case, a user needs to close an opened report by simply pressing the Escape (Esc) key. This can be done by adding macro code in Report_KeyPress or Report_KeyDown event.
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Suggested Courses

834 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