我所拥有的是一个显示公司信息的表格,以及显示联系信息的多个联系子表格,由联系人的工作职责分开(标签)。
我在公司表单上有一个显示工作职责的组合框,一旦选择了职责,它将只显示具有所选职责的联系人的公司记录。这是代码;
Sub SetFilter()
Dim ASQL As String
If IsNull(Me.cboshowcat) Then
' If the combo and all check boxes are Null, use the whole table as the RecordSource.
Me.RecordSource = "SELECT company.* FROM company"
Else
ASQL = "SELECT company.* FROM company INNER JOIN Contacts ON company.company_id = Contacts.company_id WHERE Contacts.responsibility= '" & cboshowcat & "'"
Form_Startup.RecordSource = ASQL
End If
End Sub
公司表只会显示cboshowcat组合框中指定类型的联系人的记录,而不会在主表单上显示重复的公司。
然后,我想根据主窗体上的复选框应用一些其他过滤器,这些复选框与contacts子窗体上的字段相关。它们由一个按钮以及上面的记录源代码激活;
Private Sub Command201_Click()
If Nz(Me.cboshowcat) = "" And Me.Check194 = True Or Nz(Me.cboshowcat) = "" And Me.Check199 = True Or Nz(Me.cboshowcat) = "" And Me.Check205 = True Then
MsgBox "Please Select a Job Responsibility"
Cancel = True
Else
SetFilter
If Me.Check194 = True Then
Me.Filter = "cedit <=Date()-90"
Me.FilterOn = True
Else
Me.Filter = ""
Me.FilterOn = False
If Me.Check199 = True Then
Me.Filter = "((copt)='No')"
Me.FilterOn = True
Else
Me.Filter = ""
Me.FilterOn = False
If Me.Check205 = True Then
Me.Filter = "exsite is null"
Me.FilterOn = True
Else
Me.Filter = ""
Me.FilterOn = False
End If
End If
End If
End If
End Sub
此时按钮按类别过滤掉所选联系人,但如果选中多个复选框,则仅使用其中一个过滤器。如何根据勾选的勾选框使用多个过滤器?
答案 0 :(得分:0)
如果要过滤多个条件,则需要构造Filter字符串值以反映该值。例如:
Me.Filter = "cedit <=Date()-90 AND exsite is null"
从一个空字符串开始,然后浏览复选框,根据需要构建字符串:如果在该过程结束时有一个过滤字符串,则应用它。
以下是有关如何有效执行此操作的建议:
Dim sFilter as String
sFilter=""
'...
If Me.Check194 Then BuildFilter sFilter, "cedit <=Date()-90"
If Me.Check205 Then BuildFilter sFilter, "exsite is null"
If Me.Check199 Then BuildFilter sFilter, "((copt)='No')"
'... more checkboxes...
Me.FilterOn = Len(sFilter)>0
Me.Filter = sFilter
'....
Sub BuildFilter(byRef sFilt as string, sAdd as string)
If len(sFilt)>0 Then sFilt = sFilt & " AND "
sFilt = sFilt & sAdd
End sub