带有列表框Microsoft Access的多项选择

时间:2018-08-08 18:58:45

标签: vba list ms-access filter

我正在尝试使用列表框过滤拆分表格,但始终收到错误“ 3075”。

我使用了调试工具,并且得到了:

strSearch =“ 52185A,515674B”

Task =“ select t from tOrder where([(52185A,515674B)中的[OrderID]])”

我敢肯定我会丢失一些报价,但是我似乎无法弄清楚哪里。它们都是短文本格式。

Option Compare Database

Private Sub cmdSearch_Click() 
Dim varltem As Variant 
Dim strSearch  As String 
Dim Task As String

For Each varltem In Me!LstMatricule.ItemsSelected 
   strSearch = strSearch & "," & Me!LstMatricule.ItemData(varltem) 
Next varltem
If Len(strSearch) = 0 Then 
   Task = "select * from tOrder" 
Else 
   strSearch = Right(strSearch, Len(strSearch) - 1) 
   Task = "select * from tOrder where ((OrderID] in (" & strSearch & "))" 
End If 
DoCmd.ApplyFilter Task 

End Sub 

1 个答案:

答案 0 :(得分:1)

strSearch = strSearch & ",'" & Me!LstMatricule.ItemData(varltem) & "'"

很难看到,但是基本上我在每个varItem周围添加了单引号。您希望结果看起来像这样:

strSearch = "'52185A', '515674B'"

我个人会这样做:

strSearch = strSearch & IIf(Len(strSearch) = 0, "", ",") & "'" & Me!LstMatricule.ItemData(varltem) & "'"

如果要添加2-3个以上的ID,但比稍后删除开头的逗号更干净的话,在计算上会稍微贵一些。