DAO.Recordset.RecordCount 属性 未按预期工作

DAO.Recordset.RecordCount property not working as expected

我运行正在根据表单输入进行查询。它是从 1 table 中提取记录,其中 table 与其他 3 个连接。
它确实成功提取了记录。在这种情况下,只有 1 条记录,查询确实找到了它。 问题是每次我 运行 这段代码,我都会多获得一个 RecordCount。 所以我第一次 运行 它时,RecordCount 是 1 并且 debug.print 给了我正确的信息。第二次我 运行 它时,RecordCount 是 2 并且 debug.print 给了我正确的信息但是两次。第三次……3和3,等等。 关闭表单(从中获取变量)并重新加载它并不重要。这个数字一直在攀升。我关闭 Access 并重新打开它,数字一直在攀升 - 它没有重置为找到 1 条记录。

查询根据 ProductID 选择记录,其中 (qtyordered - qtyproduced > 0) 并且记录按客户的优先级排序。

Dim rs As DAO.Recordset
    Dim db As DAO.Database

    Dim findCSQL As String

    findCSQL = "SELECT tblWarehouseTransfers.WTRProductID, tblOrderDetails.ODEPriority, tblOrderDetails.ODEQuantityOrdered, " _
                        & " tblOrderDetails.ODEQtyProduced, tblCustomers.CompanyName " _
                & " FROM  tblCustomers INNER JOIN (tblOrders INNER JOIN (tblWarehouseTransfers INNER JOIN tblOrderDetails " _
                        & " ON tblWarehouseTransfers.WTRProductID = tblOrderDetails.ODEProductFK) " _
                        & "  ON tblOrders.ORDOrderID = tblOrderDetails.ODEOrderID) ON tblCustomers.ID = tblOrders.ORDCustomerID " _
                & " WHERE (((tblWarehouseTransfers.WTRProductID) = " & Chr$(34) & Me!cboTransferProductID & Chr$(34) & ")) " _
                & " AND ((tblOrderDetails.ODEQuantityOrdered - tblOrderDetails.ODEQtyProduced)> 0) " _
                & " ORDER BY tblOrderDetails.ODEPriority "


    Set rs = db.OpenRecordset(findCSQL)

    rs.MoveFirst
    Debug.Print rs.RecordCount

    Do While Not rs.EOF
      Debug.Print ("product ID: " & rs!WTRProductID & " qty ordered: " & rs!ODEQuantityOrdered & " customer name:  " & rs!CompanyName)
      rs.MoveNext
    Loop

    rs.Close
    Set rs = Nothing

如何清空记录集并在每次子例程 运行 时重新开始?

我可能已经忘记了很久没有使用过 RecordSets,但我认为在 RecordCount 属性 给你正确的信息之前你必须遍历记录。

所以这样试试:

rs.MoveLast
Debug.Print rs.RecordCount
rs.MoveFirst

来自How to: Count the Number of Records in a DAO Recordset

The value of the RecordCount property equals the number of records that have actually been accessed.