访问VBA:如何通过'No Current Record'移动到下一个记录'Item'='Item'

时间:2013-11-04 16:50:43

标签: vba ms-access access-vba

如何将'无当前记录'移至下一个记录'Item'='Item'

循环返回并在rsSO中找到销售订单记录,但在rsInv或库存记录集中找不到,创建“未找到记录错误”。原因是,一旦通过将库存分配到打开的销售订单而耗尽库存,我就会删除该特定商品的库存记录,但仍然可以打开该商品的销售订单。一旦库存在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

1 个答案:

答案 0 :(得分: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