我正在尝试使用Access制作一个小型库存程序,但我没有太多的知识,无论我到目前为止所做的是使用Google搜索。我已经设法使用未绑定的表单创建代码来存储数据,现在我想通过以未绑定的形式为另一个表输入的数据更新表的某些特定字段。有问题的2个表是交易和股票。股票有3个字段PartNo,Location和Qty。我希望库存中的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的表中,该表只是从一个地方移动到另一个地方的记录历史记录。我想要的是表格股票应该使用这个表格更新,所以如果PartNo 2288从SAWRH001转移到SAWRH005那么它应该自动更新表格股票。据我所知,我需要嵌入一个SQL查询,但我不知道如何在VBA中这样做。
答案 0 :(得分: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
答案 1 :(得分:0)
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
请参阅链接here以获取示例代码。