【问题标题】:Sequentially updating inventory values via Access VBA通过 Access VBA 顺序更新库存值
【发布时间】:2013-11-04 23:57:45
【问题描述】:

我需要按批次#-位置从库存表中逐项减去安全库存,最大数量在前,直到安全库存耗尽。

我可以拥有多个具有不同 Lot# Location Qty 组合的库存商品。

唯一的关系是项目编号

我认为减去安全库存然后更新库存表的循环出错了。如果有更好的方法,请告诉我。

任何帮助将不胜感激。

Item       Safety_Stock
011901              917

Item         Location   Lot          QOH
011901       PR501106   REXI0474    3325
011901       pp46321b   REXI0474     475
Public Function InventoryUpdate()
Dim intTot As Long
Dim i As Integer
Dim i2 As Integer
Dim loopCounter As Integer

'Assign recordsets

'Define recordset to get expected SS data
Dim rsSS As DAO.Recordset
Set rsSS = Currentdb.OpenRecordset("SELECT * FROM tbl_ItemxSS")

'Define recordset to get Inventory data
'Inventory records ID, Site, PL, Item, Desc, Location, Lot, QOH, QtyAlloc, Created, Expire, Status
Dim rsInv As DAO.Recordset
Set rsInv = Currentdb.OpenRecordset("SELECT * FROM tbl_Inventory")

' get rsSS.recordcount and go back to the beginning
rsSS.MoveLast
rsSS.MoveFirst
'Debug.Print rsSS.RecordCount


' Need to update Inventory records returned by subtracting SS 
Dim RA() As Variant
ReDim RA(0 To rsSS.RecordCount - 1, 0 To 1)

' Populate the array with the SS data
i = 0
Do Until rsSS.EOF
'Debug.Print rsSS.Fields(0)
'Debug.Print rsSS.Fields(1)
    RA(i, 0) = rsSS!Item
    RA(i, 1) = rsSS!Safety_Stock

    If rsSS.RecordCount <> 0 Then
        rsSS.MoveNext
        i = i + 1

    End If
Loop

intTot = 0
loopCounter = 0 ' This will ensure we don't check transactions more than once

Do Until rsInv.EOF
Debug.Print rsInv.Fields(3)
 Debug.Print rsInv.Fields(7)

    If intTot < rsInv!QOH Then                      'if 0 is less than QOH
        For i = loopCounter To UBound(RA)           'Loop through SS array one by one
            intTot = intTot + RA(i, 1)              'Initialize intTot to be SS Qty
            If intTot <= rsInv!QOH Then             'If SS Qty <= QOH
                rsInv.Edit                          'Edit Inventory Table
                rsInv!QOH = rsInv!QOH - intTot      'Subtract SS from QOH
                rsInv.Update                        'Update that QOH's with new Qty
                intTot = 0                          'Reset SS qty to 0 since it was all allocated
                loopCounter = loopCounter + 1       'increase this so we don't double check a transaction
                Exit For ' exit loop and move to the next SS Qty
            End If
        Next i
    Else
        rsInv.Edit
        rsInv!QOH = rsInv!QOH
        rsInv.Update
        intTot = intTot - rsInv!QOH
    End If
    If rsInv.RecordCount <> 0 Then
        rsInv.MoveNext
    End If
Loop
End Function

【问题讨论】:

  • 错误是什么?或者发生了什么不应该发生的事情,反之亦然?
  • 没有错误信息。但是代码不会从现有数量中减去安全库存并使用新数量进行更新。在遍历 Inventory 表后,它不会更新手头的数量。即:逐项遍历库存表,并从现有数量中减去相关项目的安全库存,然后继续。我不确定我缺少什么才能让这一切正确发生。
  • 我会尝试在rsInv!QOH = rsInv!QOH - intTot 行之后使用Debug.Print rsInv!QOH,并继续在应该返回/更新值的任何内容上使用Debug.Print。然后使用即时窗口查看是否一切正常。试一试。
  • 根据您的建议,我可以看到代码从每一行中减去了一些安全库存。似乎我需要使用一行代码来确保它正在将库存项目与安全库存项目进行比较?我不是 100% 确定,但我认为这可能是个问题。我的代码中似乎缺少这点吗?如果是这样,我可以添加以下内容: If RA(i, 0) = rsInv!Item Then

标签: loops ms-access multidimensional-array vba


【解决方案1】:

您的代码有几个问题:

  1. 正如您在上一条评论中提到的,您在两个表之间的 [Item] 上不匹配,因此您的表更新不一定适用于正确的项目。
  2. rsInv 的 SELECT 语句不包含 ORDER BY 子句,因此您没有根据拥有最多的人来处理库存记录。
  3. 无需将rsSS 中的值转储到数组中并循环遍历数组;只需遍历 Recordset 本身。
  4. 总体而言,您的代码比需要的更复杂。
Dim rsSS As DAO.Recordset, rsInv As DAO.Recordset
Dim qdf As DAO.QueryDef
Dim intTot As Long, intReduction As Long

Set rsSS = CurrentDb.OpenRecordset( _
        "SELECT * FROM [tbl_ItemxSS]", _
        dbOpenSnapshot)
Set qdf = CurrentDb.CreateQueryDef("", _
        "SELECT * FROM [tbl_Inventory] " & _
        "WHERE [Item]=[pCurrentItem] " & _
        "ORDER BY [QOH] DESC")
Do Until rsSS.EOF
    intTot = rsSS!Safety_Stock
    qdf!pCurrentItem = rsSS!Item  ' set query parameter for this iteration
    Set rsInv = qdf.OpenRecordset(dbOpenDynaset)
    Do Until rsInv.EOF
        intReduction = IIf(rsInv!QOH > intTot, intTot, rsInv!QOH)
        rsInv.Edit
        rsInv!QOH = rsInv!QOH - intReduction
        rsInv.Update
        intTot = intTot - intReduction
        If intTot = 0 Then
            Exit Do
        End If
        rsInv.MoveNext
    Loop
    rsInv.Close
    Set rsInv = Nothing
    rsSS.MoveNext
Loop
Set qdf = Nothing
rsSS.Close
Set rsSS = Nothing

【讨论】:

  • 您的解决方案完美运行。谢谢你的澄清。循环通过记录集而不是数组进一步简化了解决方案。我很欣赏你的专业知识。谢谢。
猜你喜欢
  • 1970-01-01
  • 2015-08-14
  • 1970-01-01
  • 2019-01-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-14
  • 2016-01-19
相关资源
最近更新 更多