访问 VBA 过滤器 RecordCount 未正确更新值

Posted

技术标签:

【中文标题】访问 VBA 过滤器 RecordCount 未正确更新值【英文标题】:Access VBA Filter RecordCount not properly updating value 【发布时间】:2017-09-03 00:59:24 【问题描述】:

我有一个表单,我在其上构建了一个过滤器搜索,运行“after_update”。当过滤器没有记录时,表单失败并显示为空白。为了解决这个问题,我发现有几篇文章建议添加“RecordCount”并使用“if”语句来触发过滤器,如果它不是

例子:

过滤器 1:结果为 14 条记录,debug.print 记录数显示 1

过滤器 2:结果为 22 条记录,debug.print 记录数显示 14

过滤器 3:结果为 0 条记录,Form 失败,debug.print 记录数显示 22

代码:

Private Sub ApplyFilterBtn_Click()
On Error GoTo Err_ApplyFilterBtn_Click

Dim stFilter As String

stFilter = ""

If Nz(Me.FilterOwner, "") <> "" Then
    stFilter = stFilter & "[MachineOwner] = " & Me.FilterOwner & " AND "
End If

If Nz(Me.FilterType, "") <> "" Then
    stFilter = stFilter & "[MachineType] = " & Me.FilterType & " AND "
End If

If Nz(Me.FilterSubType, "") <> "" Then
    stFilter = stFilter & "[MachineSubType] = " & Me.FilterSubType & " AND "
End If

If Nz(Me.FilterMake, "") <> "" Then
    stFilter = stFilter & "[Make] Like '" & Me.FilterMake & "' AND "
End If

If Nz(Me.FilterModel, "") <> "" Then
    stFilter = stFilter & "[Model] Like '*" & Me.FilterModel & "*' AND "
End If

If Nz(Me.FilterSN, "") <> "" Then
    stFilter = stFilter & "[SN] Like '" & Me.FilterSN & "' AND "
End If

If Nz(Me.FilterStatus, "") <> "" Then
    stFilter = stFilter & "[NewStatus] = " & Me.FilterStatus & " AND "
End If



If stFilter <> "" Then
    stFilter = Left(stFilter, Len(stFilter) - 5) 'Remove the extra AND

'<<<<<<<Issue starts here<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    Me.Recordset.Clone

      Me.RecordsetClone.Filter = stFilter
        Me.RecordsetClone.MoveLast
'       Me.RecordsetClone.MoveNext  'Tried this - did not help
'        Me.RecordsetClone.Requery  'tried this - did not help

'        Me.Filter = stFilter    ' Tries this - did not help

        'debugging to see value of RecordCount
        Debug.Print stFilter
        Debug.Print Me.RecordsetClone.RecordCount

'>>>>>> Recordsetclone.RecordCount value not refreshing properly above, shows previously called filter record count

  If Me.RecordsetClone.RecordCount < 1 Then  ' no records, don't filter
        'If Not (Me.RecordsetClone.BOF And Me.RecordsetClone.EOF) Then  'work around attempt 2 - failed
        'If Me.NoMatch Then                                             'Work around Attempt 3 - failed
            RemoveFilterBtn_Click ' Call sub that Clears filter
            MsgBox "Filter Results in No records", vbOKOnly, "No Results"
        Else ' there are records, turn on filter
            Me.Filter = stFilter
            Me.FilterOn = True
        End If 'Me.RecordCount < 1
    Else
        Me.FilterOn = False
        RemoveFilterBtn_Click ' Clears filter
    End If 'stFilter <> ""

Exit_ApplyFilterBtn_Click:
        Exit Sub

Err_ApplyFilterBtn_Click:
        MsgBox Err.Description
        Resume Exit_ApplyFilterBtn_Click

End Sub


Private Sub RemoveFilterBtn_Click()
On Error GoTo Err_RemoveFilterBtn_Click

    'Sets Filter Field Values to Blank
    Me.FilterOwner = ""
    Me.FilterType = ""
    Me.FilterMake = ""
    Me.FilterModel = ""
    Me.FilterSN = ""
    Me.FilterStatus = ""
    Me.FilterSubType = ""

    'Removes Filter
    Me.Filter = ""
    Me.FilterOn = False

Exit_RemoveFilterBtn_Click:
    Exit Sub

Err_RemoveFilterBtn_Click:
    MsgBox Err.Description
    Resume Exit_RemoveFilterBtn_Click

End Sub

【问题讨论】:

rs.MoveLast THEN rs.recordcount :) 我不确定你的意思。 Recordsetclone.recordcount 在 .MoveLast 之后? MoveLast 如果没有记录将失败。所以... 啊,我为尝试更新“RecordCount”而添加的“MoveLast”也正在创建失败...... 【参考方案1】:

它更简单,但你需要正确的语法:

Dim rst As DAO.Recordset
Dim rstFiltered As DAO.Recordset

Set rst = Me.RecordsetClone
rst.Filter = stFilter

Set rstFiltered = rst.OpenRecordset()
If rstFiltered.RecordCount > 0 Then

    ' Do stuff.

End If
rstFiltered.Close

Set rstFiltered = Nothing
Set rst = Nothing

【讨论】:

谢谢!这个解决方案似乎有效。我以前没有使用过“OpenRecordset”。我不确定它在做什么,但我知道这种行为是正确的!

以上是关于访问 VBA 过滤器 RecordCount 未正确更新值的主要内容,如果未能解决你的问题,请参考以下文章

访问过滤器 VBA

访问VBA - 使用书签删除过滤器并保持当前记录

在 VBA 访问子窗体中应用错误的日期过滤器

在vba中使用多个字符串过滤访问报告,获取数据类型不匹配

访问vba docmd.applyfilter了解日期

将名称添加到日期范围搜索。微软访问 VBA