Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ms-access/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用vba代码从Multiselect列表框中选择输入的值_Vba_Ms Access_Listbox_Ms Access 2010_Listboxitem - Fatal编程技术网

如何使用vba代码从Multiselect列表框中选择输入的值

如何使用vba代码从Multiselect列表框中选择输入的值,vba,ms-access,listbox,ms-access-2010,listboxitem,Vba,Ms Access,Listbox,Ms Access 2010,Listboxitem,我有一个文本框和一个具有以下值的多选列表框 当我在文本框中输入值并单击搜索时,它应该在列表框中选择特定的值。我正在使用下面的代码 Private Sub Search_Click() Dim str As String Dim c As Collection Dim strArray() As String Dim intcnt As Integer str = txtAnswer.Value strArray = Split(str, ",") For Each itm In strAr

我有一个文本框和一个具有以下值的多选列表框

当我在文本框中输入值并单击搜索时,它应该在列表框中选择特定的值。我正在使用下面的代码

Private Sub Search_Click()
Dim str As String
Dim c As Collection
Dim strArray() As String
Dim intcnt As Integer

str = txtAnswer.Value
strArray = Split(str, ",")

For Each itm In strArray
lstAnswer.Selected (itm)= True
Next
End Sub
我想得到下面的结果

但它选择的是索引而不是值。比如说


如何选择值而不是索引?

您的代码按项目选择,而不是按值选择:

For Each itm In strArray 
    lstAnswer.Selected(itm) = True 
Next
要按值选择,请为每个值循环列出值,如果找到,则将找到的项标记为选中。

Private Sub Search\u Click()
Private Sub Search_Click()
Dim ItM As String
Dim c As Collection
Dim strArray() As String
Dim intcnt As Integer


strArray = Split(txtAnswer.Value, ",")

With lstAnswer
    For Each ItM In strArray
        For i = 0 To .ListCount - 1
            If .List(i) <> ItM Then
            Else
                .Selected(i) = True
                Exit For
            End If
        Next i
    Next ItM
End With

End Sub
将ItM设置为字符串 Dim c作为集合 Dim strArray()作为字符串 作为整数的Dim intcnt strArray=拆分(txtAnswer.Value,“,”) 有答案 对于strArray中的每个ItM 对于i=0到.ListCount-1 如果。列出(i)ItM然后 其他的 .所选(i)=真 退出 如果结束 接下来我 下一个ItM 以 端接头
感谢您的回复如果您能分享示例codeTrue会很有帮助,但我想您会发现自己有这个方法。但是@R3uK为你服务。