我正在从表单输入中运行查询。它是从1个表中提取记录,其中表连接到另外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
如何清空记录集并在每次运行子程序时重启?
答案 0 :(得分:0)
我的记忆可能已关闭,因为自从我使用RecordSets已经很长时间了,但我认为你必须在RecordCount属性给你正确的信息之前遍历记录。
所以试试这样:
rs.MoveLast
Debug.Print rs.RecordCount
rs.MoveFirst
来自How to: Count the Number of Records in a DAO Recordset
RecordCount属性的值等于实际访问过的记录数。