MS Access 使用 ComboBox 选择过滤器

MS Access Choosing Filter with ComboBox

我带着另一个关于 MS Access 及其 VBA 环境的问题回来了。

我目前正在 MS Access 2016 中构建数据库。 主视图 headview 有一个组合框 viewcombo 和一个子表单 listview.

我需要什么:我希望组合框根据选择的条目过滤列表视图。

我做了什么:

Private Sub ViewCombo_AfterUpdate()
On Error GoTo Proc_Error

If IsNull(Me.ViewCombo) Then

   Me.ListView.Form.Filter = ""
   Me.ListView.Form.FilterOn = False

Else

    Dim selectedOption As AccessObject
    selectedOption = Me.ViewCombo
  Select Case selectedOption

   Case selectedOption = "open"
      Me.ListView.Form.Filter = "Column1='" & "'"

End Select

End If
Proc_Exit:
   Exit Sub
Proc_Error:
   MsgBox "Error " & Err.Number & " Setting Filter:" & vbCrLf & Err.Description
   Resume Proc_Exit
End Sub

注意:如果viewCombo选择的条目是open,则Me.ListView.form.Filter = "Column1='" & "'"应该是空的,可用的条目是Open, Closed, ovedue, cancel and "as seleced

但是,Access 似乎不是这样工作的。如何正确编写 select 案例陈述?

编辑 1: ComboBox ViewCombo 的值是手动写入其中的。根据值的 selection,将在 ListView
上设置不同的过滤器 示例:
selected 值为 open
列表视图使用以下语句过滤:Column1 is empty

selected 值为 closed
Listview 使用语句 Column 1 is not empty, Column 2 contains the value 10 过滤(10 是状态的 ID,这些是与我一起工作的员工给我的,这些是内部的,对数据库没有意义)

希望这有助于澄清情况。

您将使用组合框的值,而不是组合框作为对象,类似于:

If IsNull(Me.ViewCombo.Value) Then

    Me.ListView.Form.Filter = ""
    Me.ListView.Form.FilterOn = False

Else

    Select Case Me.ViewCombo.Value
        Case Is "open"
            Me.ListView.Form.Filter = "[SomeFieldName] = 'open'"   ' or other filter value.

    End Select
    Me.ListView.Form.FilterOn = True

End If

我用你们给我的输入解决了这个问题:

Private Sub AuswahlFilter_AfterUpdate()
On Error GoTo Proc_Error

If Me.ViewCombo.Value = "All" Then
   Me.ListView.Form.Filter = ""
   Me.ListView.Form.FilterOn = False

Else
Select Case Me.ViewCombo.Value
    Case Is = "Open"
    Me.ListView.Form.Filter = "FlagOpenClosed='1'"
    Me.ListView.Form.FilterOn = True
End Select

End If
Proc_Exit:
   Exit Sub
Proc_Error:
   MsgBox "Error " & Err.Number & " when creating Filter:" & vbCrLf & Err.Description
   Resume Proc_Exit
End Sub

我在 ListView 上创建了一些额外的列。 Table listview 基于现在有额外的列,其中填充了来自 When(IsNull(Column1);1;0) 等语句的值。然后我为这些值设置过滤器。

我想有更好的方法,但是,我是 VBA 的菜鸟,所以这是我想出的最佳解决方案。如果有更好的方法,请不要犹豫,把它写在这里作为答案,我很高兴能学到新东西,也很高兴收到你们的来信。

-宁萨