【发布时间】:2018-07-18 09:43:38
【问题描述】:
我正在 Access 上创建一个表单,以根据列名“控件类型”过滤子表单。
我正在使用列表框来选择多个值进行过滤。
我还有一个按钮可以对表单执行过滤器。
我写了这段代码:
Private Sub cmdSearch_Click()
Dim varItem As Variant
Dim strSearch As String
Dim Task As String
For Each varItem In Me!listControl.ItemsSelected
strSearch = strSearch & "," & Me!listControl.ItemData(varItem)
Next varItem
If Len(strSearch) = 0 Then
Task = "select * from tblAB"
Else
strSearch = Right(strSearch, Len(strSearch) - 1)
Task = "select * from tblAB where Control_Type = '" & strSearch & "' "
End If
Me.tblAB_subform.Form.Filter = Task
Me.tblAB_subform.Form.FilterOn = True
End Sub
我收到一条运行时错误“3075”的行:
Task = "select * from tblAB where Control_Type = '" & strSearch & "' "
【问题讨论】:
-
我不认为这是您的代码停止的那一行。我认为你是假设那是负责的,对吗?我这么说是因为那行还不是查询表达式...。到目前为止,它是一个字符串,不会产生那个错误。
-
可能是 this 行给出了该错误:
Me.tblAB_subform.Form.Filter = Task。如果是这样,请在其前面添加一行:Msgbox task。 SQL 符合您的预期吗? -
不是错误的原因,但 strSearch 以“xxx,yyy,zzz”结尾,除非
Control_Type实际上包含确切的字符串“xxx,yyy,zzz”,这将不起作用。如果您的意图是匹配 xxx 或 yyy 或 zzz,则需要构造一个 IN() 子句。 -
继续@AlexK。评论说你也许可以使用
Task = "select * from tblAB where Control_Type IN ('" & Replace(strSearch, ",", "', '") & "')"- 我没有测试过所以不愿意添加作为答案,而且它比我的更多的是亚历克斯的思维模式。 :)