语法不正确 - Microsoft Access VBA 数据过滤器 - Select 大小写

Incorrect Syntax - Microsoft Access VBA data filter - Select Case

我认为这应该是一个简单的问题,但我正在努力寻找正确的写法并且 运行 没时间完成。

我有一个 Access 表单,它使用多个下拉框来过滤要在表单中显示的记录。我正在尝试再添加一个过滤器。问题是,我以前的过滤器都是字符串格式,而且它们工作得很好。新过滤器基于计算得出的文件,该文件生成处理记录的年份。所以我收到数据类型不匹配错误。我尝试使用日期格式声明一个新变量,但这给了我一个错误,提示缺少运算符。

我的目标是将 cboYearAudited 添加到过滤器列表中。这仅在从 cboStatus 下拉框中选择“完成”时适用。

这是我的代码:

        Option Explicit

Private Sub cboStatus_AfterUpdate()
 SetFilters
 Me.Requery
End Sub
Private Sub cboQuarter_AfterUpdate()
 SetFilters
 Me.Requery
End Sub
Private Sub cboManager_AfterUpdate()
cboEmployee.Requery
End Sub
Private Sub cboEmployee_AfterUpdate()
 SetFilters
 Me.Requery
End Sub
Private Sub cboYearAudited_AfterUpdate()
 cboEmployee.Requery
End Sub

Private Sub SetFilters()
Dim MyFilter As String
Dim MyFilterYear As Date
Dim c As Control

Select Case Me.cboStatus
    Case "Pending Review"
        MyFilter = "Auditor Is Null"
    Case "Completed"
        MyFilter = "AuditDate Is Not Null"
End Select

If Not IsNull(Me.cboQuarter) Then
    MyFilter = MyFilter & IIf(MyFilter = "", "", " AND ") & "[AuditName] = '" & Me.cboQuarter & "'"
End If
If Not IsNull(Me.cboEmployee) Then
    MyFilter = MyFilter & IIf(MyFilter = "", "", " AND ") & "[Adjuster] = '" & Me.cboEmployee & "'"
End If
If Not IsNull(Me.cboYearAudited) Then
    MyFilter = MyFilter & MyFilterYear & IIf(MyFilter = "", "", " AND ") & "[YearAudited] = '" & Me.cboYearAudited & "'"
End If

'MsgBox (MyFilter)
Me.Filter = False
Me.Filter = MyFilter
Me.FilterOn = True

For Each c In Me.Controls
    If c.Tag = "Status" Then
        c.Value = Null
    End If
Next c

End Sub

我尝试将字段类型更改为短文本,在这种情况下,我没有收到任何错误,但也没有任何反应。下拉框中的选择似乎没有任何作用。

Private Sub SetFilters()
Dim MyFilter As String
''Dim MyFilterYear As Date
Dim c As Control

Select Case Me.cboStatus
    Case "Pending Review"
        MyFilter = "Auditor Is Null"
    Case "Completed"
        MyFilter = "AuditDate Is Not Null"
End Select

If Not IsNull(Me.cboQuarter) Then
    MyFilter = MyFilter & IIf(MyFilter = "", "", " AND ") & "[AuditName] = '" & Me.cboQuarter & "'"
End If

If Not IsNull(Me.cboEmployee) Then
    MyFilter = MyFilter & IIf(MyFilter = "", "", " AND ") & "[Adjuster] = '" & Me.cboEmployee & "'"
End If

If Not IsNull(Me.cboYearAudited) Then
    MyFilter = MyFilter & IIf(MyFilter = "", "", " AND ") & "[YearAudited] = '" & Me.cboYearAudited & "'"
End If

'MsgBox (MyFilter)
Me.Filter = False
Me.Filter = MyFilter
Me.FilterOn = True

For Each c In Me.Controls
    If c.Tag = "Status" Then
        c.Value = Null
    End If
Next c

End Sub

嗨,

字段 MyFilterYear 是日期是否有原因? 我建议你使用字符串。

那么接下来会发生什么:

Dim MyFilter As String
Dim MyFilterYear As String
Dim MyFilterYearValue As Date

Dim c As Control

If Not IsNull(Me.cboQuarter) Then
    MyFilter = MyFilter & IIf(MyFilter = "", "", " AND ") & "[AuditName] = '" & Me.cboQuarter & "'"
End If
If Not IsNull(Me.cboEmployee) Then
    MyFilter = MyFilter & IIf(MyFilter = "", "", " AND ") & "[Adjuster] = '" & Me.cboEmployee & "'"
End If

If Not IsNull(Me.cboYearAudited) Then
    MyFilter = MyFilter & IIf(MyFilter = "", "", " AND ") & "[YearAudited] = '" & Me.cboYearAudited & "'"
End If

'Define the column where you want your filter to happen and add the relevant date
MyFilterYearValue = Date
MyFilterYear = "[FilterYear] = #" & MyFilterYearValue & "#"
MyFilter = MyFilter & " AND " & MyFilterYear

'MsgBox (MyFilter)
Me.Filter = False
Me.Filter = MyFilter
Me.FilterOn = True

For Each c In Me.Controls
    If c.Tag = "Status" Then
        c.Value = Null
    End If
Next c

Debug.Print MyFilter

结果应该是一个 SQL 字符串:

[AuditName] = '1' AND [Adjuster] = 'Mathias' AND [YearAudited] = '2022' AND [FilterYear] = #20/02/2022#

# 标记对于过滤日期很重要。