DAO 记录集过滤器功能无法使用 2 个属性进行过滤

DAO recordset filter function cannot filter with 2 properties

我在 MS Access 2010 中使用 VBA。

我目前正在尝试从包含 2 个字段的记录集中进行筛选。 但是我累了,它不会按照我想要的过滤。

但是如果我只根据一个字段进行过滤,记录集能够进行相应的过滤。

这就是我现在拥有的。

Private Function getCheckedRecordsFromDB(ByVal cmNum As String) As Boolean
    Dim rs As Recordset
    Dim rsFiltered As Recordset
    Dim iSeral As Integer

    'Gets different fields from different tables and store them into rs
    Set rs = CurrentDb.OpenRecordset("QueryMemoOutFrm")

    ' Its not working during the filtering, keeps returning nothing found
    rs.Filter = "Doctype='Outgoing' AND DocumentRef='" & cmNum & "'"
    Set rsFiltered = rs.OpenRecordset

    Do While Not rsFiltered.EOF
        ' Do Something
    Loop

    rs.Close
    Set rs = Nothing
    rsFiltered.Close
    Set rsFiltered = Nothing
End Function

我已经阅读了 MSDN 上的文档,但没有看到我哪里出错了。 (也许我漏掉了什么)

如果您通过 copy/paste 提供实际代码,则故障排除会容易得多。 话虽如此,我只是想知道您为什么要使用两个记录集? 如果你真的在真正的过滤记录集上循环,你会得到正确的答案吗?

消除线

Set rsFiltered = rs.OpenRecordset

rs 而不是 rsFiltered

上使用此代码块
Do While Not rs.EOF
    ' Do Something
Loop

我稍微更改了代码并且它起作用了。但不知道为什么。

Private Function getCheckedRecordsFromDB(ByVal cmNum As String) As Boolean
    Dim rs As Recordset
    Dim rsFiltered As Recordset
    Dim dSerial As Double

    'Gets different fields from different tables and store them into rs
    Set rs = CurrentDb.OpenRecordset("QueryMemoOutFrm")

    rs.Filter = "Doctype='Outgoing' AND DocumentRef='" & cmNum & "'"
    Set rsFiltered = rs.OpenRecordset

    ' newly added
    rsFiltered.MoveFirst

        Do While Not rsFiltered.EOF
            dSerial = rsFiltered!SerialNo
            rsFiltered.MoveNext
        Loop

    rs.Close
    Set rs = Nothing
    rsFiltered.Close
    Set rsFiltered = Nothing
End Function