我有一个连续的表单可以访问,标题中有 3 个组合框来过滤数据。目前我有一个相当长的嵌套 IF 来检查有多少组合框正在使用并涵盖所有 8 个渗透。有 3 个盒子还不错,但我可能会添加第 4 个甚至第 5 个,在这种情况下它会变得很荒谬。
有没有更简单的方法来为连续表单设置组合框过滤器?
当前代码
Private Sub filters()
Dim fstr As String, rgS As String, piS As String, hcS As String
rgS = "research_group_id = " & Me.fRG
piS = "pi_id = " & Me.fPI
hcS = "healthcat_id = " & Me.fHC
If IsNull(Me.fRG) Then
If IsNull(Me.fPI) Then
If IsNull(Me.fHC) Then
' Do Nothing
Else
fstr = hcS
Call filton(Me.Name, fstr)
End If
Else
If IsNull(Me.fHC) Then
fstr = piS
Call filton(Me.Name, fstr)
Else
fstr = piS & " AND " & hcS
Call filton(Me.Name, fstr)
End If
End If
Else
If IsNull(Me.fPI) Then
If IsNull(Me.fHC) Then
fstr = rgS
Call filton(Me.Name, fstr)
Else
fstr = rgS & " AND " & hcS
Call filton(Me.Name, fstr)
End If
Else
If IsNull(Me.fHC) Then
fstr = rgS & " AND " & piS
Call filton(Me.Name, fstr)
Else
fstr = rgS & " AND " & piS & " AND " & hcS
Call filton(Me.Name, fstr)
End If
End If
End If
End Sub
更新每个组合框后会触发上述代码。
Public Function filton(frmname As String, fstr As String)
With Forms(frmname)
.FilterOn = False
.Filter = fstr
.FilterOn = True
End With
End Function