5

我正在尝试使用 Access 制作一个小型库存程序,但我没有太多知识,到目前为止我所做的一切都是通过谷歌搜索。我已经设法编写代码来使用未绑定的表单存储数据,现在我想通过在未绑定表单中为另一个表输入的数据来更新表的某些特定字段。有问题的 2 个表是 Transaction 和 Stock。库存有 3 个字段 PartNo、Location 和 Qty。我希望从库存移出的位置中扣除库存数量,并将其添加到要到达的位置。

表中的样本数据Stock

Stock_PartNo    Stock_Location  Stock_Qty
2288            SAWRH001        85
2288            SAWRH002        54
3214            SAWRH003        544
4567            SAWRH001        32
5555            SAWRH002        128
5555            SAWRH005        874
5678            SAWRH002        321
6544            SAWRH004        465
6666            SAWRH003        45
6666            SAWRH004        87
7777            SAWRH003        365
7890            SAWRH002        352
8765            SAWRH005        57
8888            SAWRH004        54
9999            SAWRH005        21

这是我的未绑定表单代码:

Private Sub Command39_Click()

Dim db As Database, rsCust As Recordset


    Set db = CurrentDb
    Set rsCust = db.OpenRecordset("Transaction", DB_OPEN_DYNASET)

        rsCust.AddNew
        rsCust("Trans_PartNo") = Me!Combo52
        rsCust("Trans_Desc") = Me!Text19
        rsCust("Trans_Disp") = Me!Text21
        rsCust("Trans_Recv") = Me!Text23
        rsCust("Trans_Qty") = Me!Text25
        rsCust("Trans_Date") = Me!Text29
        rsCust.Update

        MsgBox "Material transfer information has been updated"

        Call ClearControls

    rsCust.Close
    db.Close
End Sub

这些数据将存储在一个名为 Transaction 的表中,该表只是从一个地方移动到另一个地方的记录历史记录。我想要的是表 Stock 应该使用这个表格来更新,所以如果 PartNo 2288 从 SAWRH001 转移到 SAWRH005 那么它应该自动更新表 Stock。据我了解,我需要嵌入一个 SQL 查询,但我不知道如何在 VBA 中做到这一点。

4

2 回答 2

4

带参数:

Dim db As Database
Dim qdf As QueryDef

Set db = CurrentDb
sSQL = "UPDATE Stock SET Stock_Qty = Stock_Qty + [p1] " & _
     " WHERE Stock_PartNo = [p2] AND Stock_Location = [p3]"

''Temporary query
Set qdf = db.CreateQueryDef("", sSQL)
''No need to worry about quotes etc
qdf.Parameters("p2") = Me!Combo52

''Subtract
qdf.Parameters("p1") = Me.Text25 * -1
qdf.Parameters("p3") = Me.From
qdf.Execute dbFailOnError

''Add
qdf.Parameters("p1") = Me.Text25
qdf.Parameters("p3") = Me.To
qdf.Execute dbFailOnError
于 2012-09-21T10:34:11.370 回答
1
db.Execute "UPDATE Transaction SET Stock_Qty = Stock_Qty - " & Val(Me!Text25) _
& " WHERE Stock_PartNo = '" & Me!Combo52 _
& "' AND Stock_Location = '" & fromLocation & "'", dbFailOnError

db.Execute "UPDATE Transaction SET Stock_Qty = Stock_Qty + " & Val(Me!Text25) _
& " WHERE Stock_PartNo = '" & Me!Combo52 _
& "' AND Stock_Location = '" & toLocation & "'", dbFailOnError

请参阅此处的链接以获取示例代码。

于 2012-09-21T09:41:38.157 回答