0

如何将“无当前记录”移到“项目”=“项目”的下一个记录

循环返回并在 rsSO 中找到销售订单记录,但在 rsInv 或 Inventory 记录集中找不到,从而创建“未找到记录错误”。原因是一旦库存通过分配它以打开销售订单而耗尽,我然后删除该特定项目的库存记录,但我仍然可能为该项目留下未打开的销售订单。一旦 rsInv 中的库存用完,我如何移动到未结销售订单 rsSO 记录集中的下一个项目?

它发生在第一个循环内的以下代码部分中:

     Do Until rsInv!Item = rsSO!Item
       If rsInv!Item = rsSO!Item Then
       Exit Do
       Else
       rsInv.MoveNext
       End If
    Loop

完整代码:

Public Function UpdateInventoryIntl()

Dim rsInv As DAO.Recordset, rsSO As DAO.Recordset, db As DAO.Database
Dim qdf As DAO.QueryDef
Dim AllocationQty As Long, SaleOrderRemainder As Long
Set db = CurrentDb

    Set rsInv = CurrentDb.OpenRecordset( _
            "SELECT * FROM [tbl_InventoryAvailForIntl] ORDER BY [Item] DESC,[QOH_IntlAllocation] DESC", _
            dbOpenDynaset)

    Set rsSO = CurrentDb.OpenRecordset("SELECT * FROM [tbl_IntlAllocated] ORDER BY [Item] DESC,[Qty_Open] DESC", _
            dbOpenDynaset)


    Do Until rsSO.RecordCount = 0

             Do Until rsInv!Item = rsSO!Item
               If rsInv!Item = rsSO!Item Then
               Exit Do
               Else
               rsInv.MoveNext
               End If
            Loop


        AllocationQty = IIf(rsSO!Qty_Open > rsInv!QOH_IntlAllocation, rsInv!QOH_IntlAllocation, rsSO!Qty_Open)

        db.Execute ("INSERT INTO tbl_IntlAllocatedResults (Due_Date, Sale_Order_Num, SO_Line, Item, Qty_OpenStart, Location, Lot, QtyAllocated) " & _
        "VALUES (#" & rsSO!Due_Date & "#,'" & rsSO!Sale_Order_Num & "'," & rsSO!SO_Line & ",'" & rsSO!Item & "'," & rsSO!Qty_OpenStart & ",'" & rsInv!Location & "','" & rsInv!Lot & "'," & AllocationQty & ");")

        rsSO.Edit
        rsSO!Qty_Open = rsSO!Qty_Open - AllocationQty
        rsSO.Update

        If rsSO!Qty_Open = 0 Then
        rsSO.Delete
        rsSO.MoveNext
        End If

        rsInv.Edit
        rsInv!QOH_IntlAllocation = rsInv!QOH_IntlAllocation - AllocationQty
        rsInv.Update
        Debug.Print rsInv!QOH_IntlAllocation

        If rsInv!QOH_IntlAllocation = 0 Then
        rsInv.Delete
        rsInv.MoveNext
        End If

    Loop

rsSO.Close
Set rsSO = Nothing
Set qdf = Nothing
rsInv.Close
Set rsInv = Nothing

End Function
4

1 回答 1

0

而不是循环通过记录集使用 FindFirst:

Dim sCriteria as String

sCriteria = "Item = " & rsSO!Item
rsInv.FindFirst (sCriteria)
If rsInv.NoMatch Then
    ' Do whatever you need to if there is no inventory
Else
    ' Carry on with your code
End If

根据记录集的大小,您可以根据需要获取更好的记录集效率。

最初不要设置您的 rsInv,然后不要使用有问题的循环:

Set rsInv = Currentdb.OpenRecordset( _
    "SELECT * FROM [tbl_InventoryAvailForIntl] _
        WHERE [Item] = " & rsSO!Item & " ORDER BY [QOH_IntlAllocation] DESC", _
        dbOpenDynaset)

然后您可以测试是否没有记录:

If rsInv.EOF and rsInv.BOF Then
    ' No records, do what is required when no inventory
End If
于 2013-11-05T00:38:30.673 回答