SelStart and SelLength don't work right away

chchad

New member
Local time
Today, 13:46
Joined
Dec 7, 2012
Messages
6
There's a textbox "blockcontent" and a listbox "blocks". The user enters some text into blockcontent, then goes to blocks, clicks on some item and its text is added to the current cursor position (stored in a global variable) in blockcontent.
Code:
Private Sub blockcontent_Exit(Cancel As Integer)
    cur_start = blockcontent.SelStart
    cur_len = blockcontent.SelLength
End Sub
But! SelStart and SelLength work only when the selected text was typed in one of previous sessions of blockconent being in focus. I.e. if I typed something, selected any of it and try to use insertion, SelStart and SelLength would give 0 and 0.
Is there any way to make them work with any text?
 
Read through and didn't see any similarity. These properties DO work, but not right away. This behavior reminds of a "Dirty" property in a way, however. I tried
Code:
Private Sub Form_Dirty(Cancel As Integer)
DoCmd.CancelEvent
End Sub

Private Sub blockcontent_Dirty(Cancel As Integer)
DoCmd.CancelEvent
End Sub
but with no effect. And Me.Dirty effects in <You entered an expression that has an invalid reference to the property Dirty.>
 
Here's more from M$oft:
Code:
SelStart Property [Access 2003 VBA Language Reference]
Office 2003


The SelStart property specifies or determines the starting point of the selected text or the position of the insertion point if no text is selected. Read/write Integer.

expression.SelStart

expression Required. An expression that returns one of the objects in the Applies To list.
Remarks

The SelStart property uses an Integer in the range 0 to the total number of characters in a text box or text box portion of a combo box. You can set the SelStart property by using a macro or Visual Basic .

To set or return this property for a control, the control must have the focus. To move the focus to a control, use the SetFocus method.

Changing the SelStart property cancels the selection, places an insertion point in the text, and sets the SelLength property to 0.
Example

The following example uses two event procedures to search for text specified by a user. The text to search is set in the form's Load event procedure. The Click event procedure for the Find button (which the user clicks to start the search) prompts the user for the text to search for and selects the text in the text box if the search is successful.

Private Sub Form_Load()
    
    Dim ctlTextToSearch As Control
    Set ctlTextToSearch = Forms!Form1!Textbox1
    
    ' SetFocus to text box.
    ctlTextToSearch.SetFocus
    ctlTextToSearch.Text = "This company places large orders twice " & _
                           "a year for garlic, oregano, chilies and cumin."
    Set ctlTextToSearch = Nothing
    
End Sub

Public Sub Find_Click()
    
    Dim strSearch As String
    Dim intWhere As Integer
    Dim ctlTextToSearch As Control
    
    ' Get search string from user.
    With Me!Textbox1
        strSearch = InputBox("Enter text to find:")
        
        ' Find string in text.
        intWhere = InStr(.Value, strSearch)
        If intWhere Then
            ' If found.
            .SetFocus
            .SelStart = intWhere - 1
            .SelLength = Len(strSearch)
        Else
            ' Notify user.
            MsgBox "String not found."
        End If
    End With
    
End Sub
 
Yeah, I've read it all. But in MS example the text in the textbox is predefined. In my case the program is trying to determine the position of cursor in justnowtypedin text. And that's where the problem shows up. When the text had been typed in in one of previous sessions of textbox having the focus, everything is just as all right as in the example.
 
I'm missing your point unfortunately. I think what it is saying is you could find the length of the text in the textbox using NumChars = Len(Trim(Me.textbox))

and then use NumChars to position the cursor

Me.textbox.selLength = NumChars

Just my best guess without setting up a test.
 
No. Look. I need to find out where the cursor is - in order to insert some text there via VBA.
Attempt one. I type in some text in the textbox, move focus to another control, come back, put the cursor somewhere in the text. Ask for SelStart in OnExit. Okay, I've got it.
Attempt two. I assign some default text to that textbox. I put the cursor somewhere in the text. Ask for SelStart in OnExit. Okay, I've got it.
Attempt three. I type in some text, leave the cursor somewhere, ask for SelStart in OnExit. It says 0!
 
Post a copy of your db without any confidential info. Just enough data to show the problem.
 
Attached. Try selecting some text in Form 1's blockcontent textbox, then leaving it (moving focus elsewhere), then typing in some text, selecting it and leaving textbox.
Thank you.
 

Attachments

Having had a bit of a play with this in my sand box DB; It seems that VBA is unable to determine the location of the cursor (using the On Exit, or Lost Focus events) when data has just been entered into the field, the Before Update event will however work in this instance, but not if no alteration has been made to the data in the field.

You may need to rethink how you are going to achieve your end goal.
 
So the suggested function works for an active control. I guess I'll actually have to rethink my mechanism... Like making a shortcut for textbox (while it has the focus) that'll insert the item that's selected in the listbox. Vice versa, that is. Thank you, guys :)
Problem solved.
 

Users who are viewing this thread

Back
Top Bottom