Reverse ComboBox:如何根据 TextBox 值以编程方式 select ActiveX ComboBox 中的项目?
Reverse ComboBox: How to programmatically select an item in ActiveX ComboBox based on a TextBox value?
我有一个多列组合框,现在尝试搜索代码,但无济于事。
我的组合框如下所示:
emp_id emp_name
--------- ---------------
<blank> - Select -
1 James Bond
2 Jason Statham
3 Sylvester S.
4 Chuck Norris
where columnwidths = "0,100" and and columnheads = False, 所以它只会显示 emp_name
我想要做的是,以编程方式select并显示基于变量
的组合框中的项目
到目前为止,最接近我想要实现的逻辑是:
empSysID = 3 '~This is Sylvester S.
With .ComboBoxEmployee
For i = 0 To .ListCount - 1
If .Column(0, i) = empSysID Then
.Value = .List(i) '~Trying to select and display 'Sylvester S.' in combobox
Exit For
End If
Next i
End With
但我收到 运行 时间错误“380”:无法设置值 属性。无效 属性 值。
提前致谢!
您需要使用ListIndex
:
empSysID = 3 '~This is Sylvester S.
With .ComboBoxEmployee
For i = 0 To .ListCount - 1
If .Column(0, i) = empSysID Then
.ListIndex = i
Exit For
End If
Next i
End With
我有一个多列组合框,现在尝试搜索代码,但无济于事。
我的组合框如下所示:
emp_id emp_name
--------- ---------------
<blank> - Select -
1 James Bond
2 Jason Statham
3 Sylvester S.
4 Chuck Norris
where columnwidths = "0,100" and and columnheads = False, 所以它只会显示 emp_name
我想要做的是,以编程方式select并显示基于变量
的组合框中的项目到目前为止,最接近我想要实现的逻辑是:
empSysID = 3 '~This is Sylvester S.
With .ComboBoxEmployee
For i = 0 To .ListCount - 1
If .Column(0, i) = empSysID Then
.Value = .List(i) '~Trying to select and display 'Sylvester S.' in combobox
Exit For
End If
Next i
End With
但我收到 运行 时间错误“380”:无法设置值 属性。无效 属性 值。
提前致谢!
您需要使用ListIndex
:
empSysID = 3 '~This is Sylvester S.
With .ComboBoxEmployee
For i = 0 To .ListCount - 1
If .Column(0, i) = empSysID Then
.ListIndex = i
Exit For
End If
Next i
End With