简体   繁体   English

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

[英]Incorrect Syntax - Microsoft Access VBA data filter - Select Case

I think this should be an easy one but I am struggling to find the correct way to write this and am running out of time to complete.我认为这应该是一个简单的问题,但我正在努力寻找正确的方法来编写它并且我没有时间来完成。

I have an Access form that uses multiple drop down boxes to filter the records to display in the form.我有一个 Access 表单,它使用多个下拉框来过滤要在表单中显示的记录。 I am attempting to add one more filter.我正在尝试再添加一个过滤器。 The issue is, my previous filters have all been String format and they work perfectly.问题是,我以前的过滤器都是字符串格式,而且它们工作得很好。 The new filter is based on a calculated filed that produces the Year that the record was worked.新过滤器基于计算得出的文件,该文件生成处理记录的年份。 So I am getting a Data Type Mismatch error.所以我收到数据类型不匹配错误。 I tried Declaring a new variable with Date format but that gave me an error that says Missing Operator.我尝试用日期格式声明一个新变量,但这给了我一个错误,提示缺少运算符。

My goal is to add cboYearAudited to the list of filters.我的目标是将 cboYearAudited 添加到过滤器列表中。 This would apply only when "Complete" was selected from the cboStatus dropdown box.这仅在从 cboStatus 下拉框中选择“完成”时适用。

Here is my code:这是我的代码:

        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

I tried changing the field type to Short text and in this case the I get no errors but also, nothing happens.我尝试将字段类型更改为短文本,在这种情况下,我没有收到任何错误,但也没有任何反应。 The selection in the drop down box does not appear to do anything.下拉框中的选择似乎没有做任何事情。

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

Hy,嗨,

Is there a reason why the field: MyFilterYear is a date?该字段是否有原因:MyFilterYear 是一个日期? I suggest you use a string.我建议你使用字符串。

So what happens then is:那么接下来会发生什么:

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

The result should be an SQL String:结果应为 SQL 字符串:

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

The # marks are important for filtering dates. # 标记对于过滤日期很重要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM