SelLength, SelStart, SelText Properties

Applies To

Combo Box Control, Text Box Control.

Description

You can use these properties for tasks such as positioning the insertion point, establishing an insertion range, selecting parts of strings in a control, and deleting text.

  • SelLength — You can use this property to specify or determine the number of characters selected in a text box control or the text box portion of a combo box.
  • SelStart — You can use this property to specify or determine the starting point of the selected text or the position of the insertion point if no text is selected.
  • SelText — You can use this property to specify or determine the string containing the selected text or a zero-length string ("") if no characters are selected.

Setting

  • SelLength, SelStart — An integer in the range 0 to the total number of characters in the control or text box portion of the combo box.
  • SelText — A string expression that equal to the text selected in the control.

You can set the SelLength and SelStart properties using only a macro or Visual Basic. The SelText property is read-only.

Remarks

To set or return these properties for a control, the control must have the focus (to move the focus to a control, you can use the SetFocus method).

Setting the SelLength property to a number less than 0 produces a run-time error.

Setting the SelStart property to a number greater than the number of characters in the control sets the property to the existing text length.

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

See Also

SetFocus Method, Text Property.

Example

The following example uses two event procedures to search for text specified by a user. The text to search through is set in the form’s Load event procedure. The Click event procedure for the Find button (which the user chooses 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.


Sub Form_Load()
    Dim ctlTextToSearch As Control
        Set ctlTextToSearch = Forms![Form1]![TextBox1]
        ctlTextToSearch.SetFocus          ' Move focus to text box.
        ctlTextToSearch.Text = _
            "This company places large orders twice a year for"
        ctlTextToSearch.Text = ctlTextToSearch.Text _
            & " garlic, oregano, chilies and cumin."Sub
Find_Click()
    Dim strSearch As String, intWhere As Integer
    Dim ctlTextToSearch As Control
    ' Get search string from user.
    Set ctlTextToSearch = Forms![Form1]![TextBox1]
        ' Move focus to text box.
        ctlTextToSearch.SetFocus
        strSearch = InputBox ("Enter text to be found:")
        ' Find string in text.
        intWhere = InStr (ctlTextToSearch.Text, strSearch)
        If intWhere Then                                   ' If found.
            ctlTextToSearch.SelStart = intWhere - 1       ' Set SelStart.
            ctlTextToSearch.SelLength = Len(strSearch)  ' Set SelLength.
        Else
            MsgBox "String not found."                   ' Notify user.
        End IfSub