ComboBox.SelText 属性 (Access)

SelText 属性将返回包含选定的文本的字符串。 读/写 String

语法

表达式SelText

表达 一个代表 ComboBox 对象的变量。

备注

如果未不选择任何文本, SelText 属性将包含一个 值。

SelText 属性使用的字符串表达式,包含在控件中选定的文本。 如果设置此属性时,控件将包含所选的文本,则新的 SelText 设置将取代这些文本。

若要设置或返回控件的这个属性,控件必须获得焦点。 若要将焦点移到某个控件,使用 SetFocus 方法。

示例

下面的示例使用两个事件过程来搜索用户指定的文本。 要搜索的文本在窗体的 Load 事件过程中设置。 “查找”按钮的 Click 事件过程 (用户单击以开始搜索) 提示用户输入要搜索的文本,并在搜索成功时选择文本框中的文本。

Sub Form_Load() 
 Dim ctlTextToSearch As Control 
 Set ctlTextToSearch = Forms!Form1!TextBox1 
 ctlTextToSearch.SetFocus ' SetFocus to text box. 
 ctlTextToSearch.SelText = "This company places large orders " _ 
 & "twice a year for garlic, oregano, chilies and cumin." 
End Sub 
 
Sub Find_Click() 
 Dim strSearch As String, 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

支持和反馈

有关于 Office VBA 或本文档的疑问或反馈? 请参阅 Office VBA 支持和反馈,获取有关如何接收支持和提供反馈的指南。