我有一个用于填充查询的表单。我在下面给出了一个代码片段。用户可以输入多个字段来缩小报告范围。我找到了下面的代码,如果用户只想为每个类别做一个选择,那么它就很有效,即" status" = C.我的问题是,在某些情况下,用户可能正在寻找多个状态。如果我现在在表单中输入C或S,则查询将填充 C或S ,这将不会产生任何结果,因为它应该是" C"或" S"或 C 或 S 。任何人都可以帮我转换这段代码,以便输入多个值并将其正确读入查询吗?
Function SearchCriteria()
Dim strSQL As String
Dim tblname, strqry As String
Dim qdf As QueryDef
Set dbs = CurrentDb
Dim allexists As QueryDef
strqry = "All Selected"
tblname = "SelectionWeeklyAuto1000"
On Error GoTo err_handler
Set allexists = dbs.QueryDefs("All Selected")
err_handler:
Select Case Err.Number
Case 3265
strSQL = "SELECT " & tblname & ".* From " & tblname & " WHERE (1=1)"
Set qdf = dbs.CreateQueryDef(strqry)
qdf.SQL = strSQL
DoCmd.Save
End Select
DoCmd.DeleteObject acQuery, strqry
Set qdf = dbs.CreateQueryDef(strqry)
strSQL = "SELECT " & tblname & ".* From " & tblname & " WHERE (1=1)"
If Not IsNull([Forms]![Report Tool]![Text1]) Then
strSQL = strSQL & " And ([Screen] Like ""*" & [Forms]![Report Tool]![Text1] & "*"")"
End If
If Not IsNull([Forms]![Report Tool]![Text2]) Then
strSQL = strSQL & " And ([Status] Like ""*" & [Forms]![Report Tool]![Text2] & "*"")"
End If
答案 0 :(得分:0)
如果您的主要问题是使用=
并且代码段使用的是Like
,那么我会将代码段扩展为
strSQL = strSQL & " And ([Status] Like ""*" & Replace([Forms]![Report Tool]![Text2], " or ", "*"" Or [Status] Like ""*") & "*"")"