简体   繁体   English

在 If 语句中使用 Like “*” 运算符

[英]Using the Like “*” operator in a If Statement

I am trying to create a filter based on four combo boxes that filter records in a subform.我正在尝试基于四个组合框创建一个过滤器,这些组合框过滤子表单中的记录。 The problem is, one value from the RowSource lists on the combo boxes do not exist in the base query.问题是,组合框上的 RowSource 列表中的一个值在基本查询中不存在。 That value is " All ".该值是“ All ”。 I was planning to use the same code on all the combo boxes AfterUpdate event.我计划在所有组合框AfterUpdate事件上使用相同的代码。 Please see my approach below and advice where necessary.请参阅下面的方法和必要的建议。 I currently get a type mismatch error on the Me.Combobox1.Value = "Like "*""我目前在Me.Combobox1.Value = "Like "*""上遇到类型不匹配错误

If IsNull(Me.combo1.Value) Or IsNull(Me.combo2.Value) Or IsNull(Me.combo3.Value) Or 
IsNull(Me.combo4.Value) Then
Exit Sub
End If

If Not IsNull(Me.combo1.Value) Or Not IsNull(combo2.Value) Or Not IsNull(combo3.Value) Or Not 
IsNull(combo4.Value) Then

If Me.combo1 = "All" Then
Me.combo1.Value = "Like " * ""
ElseIf Me.combo2.Value = "All" Then
Me.combo2.Value = "Like " * ""
ElseIf Me.combo3.Value = "All" Then
Me.combo3.Value = "Like " * ""
ElseIf Me.combo4.Value = "All" Then
Me.combo4.Value = "Like " * ""

Task1 = "SELECT * FROM qryBase WHERE [Quarter] = '" & Me.combo1.Value & "' AND [CurrentArea] = '" & 
Me.combo2.Value & "' AND [CurrentStatus] = '" & Me.combo3.Value & "' AND [MainUser] = '" & 
Me.combo4.Value & "'"

End If

End If

As is, your query cannot work as LIKE is meant to replace = in conditional statements.照原样,您的查询无法正常工作,因为LIKE旨在替换条件语句中的= However, LIKE without wildcards behaves like = .但是,没有通配符的LIKE的行为类似于= Therefore, place the LIKE operator inside the SQL statement.因此,将LIKE运算符放在 SQL 语句中。

Dim cbo1, cbo2, cbo3, cbo4 As String
...
If Me.combo1 = "All" Then
    cbo1 = "*"
ElseIf Me.combo2.Value = "All" Then
    cbo2 = "*"
ElseIf Me.combo3.Value = "All" Then
    cbo3 = "*"
ElseIf Me.combo4.Value = "All" Then
    cbo4 = "*"
End If

Task1 = "SELECT * FROM qryBase WHERE [Quarter] LIKE '" & cbo1 & "'" _
         & " AND [CurrentArea] LIKE '" & cbo2 & "'" _
         & " AND [CurrentStatus] LIKE '" & cbo3 & "'" _
         & " AND [MainUser] LIKE '" & cbo4 & "'"

Me.frmDatasheet.Form.Recordsource = Task1
Me.frmDatasheet.Form.Requery

However, because combo boxes can have apostrophes, consider parameterization to avoid need of concatenating VBA values directly into SQL but bind them as parameters:但是,因为组合框可以有撇号,所以考虑参数化以避免需要将 VBA 值直接连接到 SQL 中,但将它们绑定为参数:

Dim qdef As QueryDef
Dim rst As Recordset
Dim sql AS String
Dim cbo1, cbo2, cbo3, cbo4 As String

sql = "PARAMETERS cbo1 TEXT, cbo2 TEXT, cbo3 TEXT, cbo4 TEXT;" _
        & "SELECT * FROM qryBase WHERE [Quarter] LIKE [cbo1]" _
        & " AND [CurrentArea] LIKE [cbo2]" _
        & " AND [CurrentStatus] LIKE [cbo3]" _
        & " AND [MainUser] LIKE [cbo4]"

If Me.combo1 = "All" Then
    cbo1 = "*"
ElseIf Me.combo2.Value = "All" Then
    cbo2 = "*"
ElseIf Me.combo3.Value = "All" Then
    cbo3 = "*"
ElseIf Me.combo4.Value = "All" Then
    cbo4 = "*"
End If

' INITIALIZE QUERY OBJECT
Set qdef = CurrentDb.CreateQueryDef("", sql)
' Set qdef = CurrentDb.QueryDefs("mySavedParamQuery")

' BIND PARAMS
qdef!cbo1 = cbo1
qdef!cbo2 = cbo2
qdef!cbo3 = cbo3
qdef!cbo4 = cbo4

' SET FORM RECORDSET TO EXECUTED QUERY 
Set rst = qdef.OpenRecordset()    
Set Me.frmDatasheet.Form.Recordset = rst

Set rst = Nothing: Set qdef = Nothing

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM