总计匹配列表框选择

时间:2014-05-07 19:19:18

标签: listbox access-vba

我在表单上有一个列表框,用户在选择这些记录之前手动选择包含单个数量的行,这些行需要加起来在表单上输入的总数量。然后将选定的行发送到表格以便打印输出,以便可以正确更新物理库存。

我不确定当所选总数高于指定数量并且将差异发送到我的表时,如何减去部分数量。我已经包含了我目前设置的内容......

Private Sub ctrSend_Click()
Dim intI As Integer
Dim lst As ListBox
Dim varItem As Variant
Dim rst As DAO.Recordset
Dim qtySum As Variant
Dim qtyDiff As Variant
Dim rowMax As Variant
Dim rowUpdate As Variant

Set lst = Me![lstShipping]
Set rst = CurrentDb.OpenRecordset("ShipInv", dbOpenTable)

With lst
    If .ItemsSelected.count = 0 Then Exit Sub
        For Each varItem In .ItemsSelected
            qtySum = qtySum + .Column(3, varItem)
        Next

    If Me.[ctrQtyProd] = qtySum Then
        MsgBox "Qty Selected EQUALS Qty Being Shipped.", vbOKOnly, "Quantity Confirmation"
    ElseIf Me.[ctrQtyProd] > qtySum Then
        MsgBox "Qty Selected LESS THAN Qty Being Shipped, please select more Inventory.", vbOKOnly, "Inventory Confirmation"
    Else Me.[ctrQtyProd] < qtySum Then
        qtyDiff = qtySum - Me.[ctrQtyProd]
        .Column(3, .ItemSelected.count - 1) = .Column(3, .ItemSelected.count - 1) - qtyDiff

    End If
End With

With lst
    For Each varItem In .ItemsSelected
        rst.AddNew
        rst!Order = Me.[ctrSOrder]
        rst!EntDate = Date
        rst!ShipDate = Me.[ctrSDate]
        rst!BIN = .Column(0, varItem)
        rst!SKU = .Column(1, varItem)
        rst!Lot = .Column(2, varItem)
        rst!QtyProd = .Column(3, varItem)
        rst.Update
    Next
End With
rst.Close

Set rst = Nothing
MsgBox "Warehouse Inventory Updated", vbOKOnly, "Update Confirmation"
End Sub

1 个答案:

答案 0 :(得分:1)

这就是我想要让它做我想做的事情......

Private Sub ctrSend_Click()
Dim intI As Integer
Dim lst As ListBox
Dim varItem As Variant
Dim rst As DAO.Recordset
Dim rst2 As DAO.Recordset
Dim qtySum As Variant
Dim qtyDiff As Variant
Dim rowMax As Variant
Dim rowUpdate As Variant
Dim rowBIN As Variant
Dim sqlShip As String

Set lst = Me![lstShipping]
Set rst = CurrentDb.OpenRecordset("ShipInv", dbOpenTable)
Set rst2 = CurrentDb.OpenRecordset("Storage", dbOpenTable)
qtySum = 0
rowMax = 0

With lst
    If .ItemsSelected.count = 0 Then Exit Sub
        For Each varItem In .ItemsSelected
            qtySum = qtySum + .Column(3, varItem)
        Next

    If Me.[ctrQtyProd] = qtySum Then
        MsgBox "Qty Selected EQUALS Qty Being Shipped.", vbOKOnly, "Confirmation Message"
    ElseIf Me.[ctrQtyProd] > qtySum Then
        MsgBox "Qty Selected LESS THAN Qty Being Shipped, please select more Inventory.", vbOKOnly, "Confirmation Message"
    Else
        qtyDiff = qtySum - Me.[ctrQtyProd]
        rowMax = lst.Column(3, lst.ItemsSelected.count)
        rowUpdate = rowMax - qtyDiff

        MsgBox "Qty Successfully Updated.", vbOKOnly, "Confirmation Message"
    End If
End With

With lst
    For Each varItem In .ItemsSelected
        rst.AddNew
        rst!Order = Me.[ctrSOrder]
        rst!EntDate = Date
        rst!ShipDate = Me.[ctrSDate]
        rst!BIN = .Column(0, varItem)
        rst!SKU = .Column(1, varItem)
        rst!Lot = .Column(2, varItem)
        rst!QtyProd = rowUpdate
        rst.Update
    Next
End With
rst.Close

Set rst = Nothing
End Sub