问题描述
我使用带有两个组合框的表单来选择“主要”过滤器,然后选择“次要”过滤器来缩小范围。
就其本身而言,过滤器效果很好。我可以选择主要或次要,并相应地进行过滤。但是,我想将两者都加入过滤器以仅显示符合条件的记录。它应该是一个简单的 AND...但是...
代码如下:
Private Sub cmbSecondaryFilter_Click()
If Not IsNull(Me.cmbShowOnly.Value) Then ' making sure that the primary focus combo Box isn't null
subFilter = Me.ActiveControl.Column(0)
classDept = Me.cmbShowOnly
secondFilter = "Class_Department= '" & classDept & "'" 'this returns the correct filter criteria
secondFilterB = "Current_Class_Number= '" & subFilter & "'" 'this returns the correct filter criteria
Me.Filter = "" ' clear filter,chainging the secondary filter requires this to happen
Me.FilterOn = True ' enable that filter - filter is clear
'set filter
'Me.Filter = secondFilter & " AND " & secondFilterB ' add new filter ' this fails
Me.Filter = "Me.[Class_Department]= '"& classDept & "' AND Me.[Current_Class_Number] = '"& subFilter & '" ' this fails as well.
' how do I AND the two filters?
Me.FilterOn = True ' enable the new filter
Else ' if primary filter is blank,exit sub
MsgBox "Select a primary filter first.",vbOKOnly
Me.cmbSecondaryFilter = ""
Exit Sub
End If
End Sub
提前致谢!感谢您的帮助。
解决方法
Private Sub cmbSecondaryFilter_Click()
Dim secondaryFilter As String
If Not IsNull(Me.cmbShowOnly.Value) Then ' making sure that the primary focus combo box isn't null
secondaryFilter = secondaryFilter & "([Class_Department]=""" & Me.cmbShowOnly & """) AND "
End If
If Not IsNull(Me.cmbSecondaryFilter.Value) Then
secondaryFilter = secondaryFilter & "([Current_Class_No] = """ & Me.cmbSecondaryFilter & """) "
End If
'set filter
Me.Filter = secondaryFilter
Me.FilterOn = True ' enable the new filter
我有一个 ClearFilter 按钮,允许用户选择重置。此外,如果他们选择新的 cmbShowOnly,该代码也会清除 cmbSecondaryFilter。
我使用您提供的数据库作为指南。我是对的,撇号/引号最初是对我的。
感谢您的帮助和指向正确的方向!