I have the public subroutine as follows to add or edit the "Stocks" table.
The public subroutine "UpdateStocks" is called by the click event of the confirmed button on the purchase order.
The purchase order has a purchase details sub-form(called "進貨單維護子表單"), which contains the "ProductID" field and "Quantity" field. The purchase order also has a "WarehouseID" field. These three fields are the parameters to the public subroutine to add or edit the alike three fields ("ProductID"、"WarehouseID" and "Quantity") of the "Stocks" table.
But the result of the execution is that the "Stocks" table is not updated.
The click event of the confirm order button of the purchase order is as follows:
The public subroutine "UpdateStocks" is called by the click event of the confirmed button on the purchase order.
The purchase order has a purchase details sub-form(called "進貨單維護子表單"), which contains the "ProductID" field and "Quantity" field. The purchase order also has a "WarehouseID" field. These three fields are the parameters to the public subroutine to add or edit the alike three fields ("ProductID"、"WarehouseID" and "Quantity") of the "Stocks" table.
But the result of the execution is that the "Stocks" table is not updated.
The click event of the confirm order button of the purchase order is as follows:
Code:
Private Sub cmdConfirm_Click()
If Me.NewRecord Or Me.Dirty Or _
Me.進貨單維護子表單.Form.NewRecord Or Me.進貨單維護子表單.Form.NewRecord Then
Exit Sub
End If
If Me.Confirm = True Or IsNull(Me.Confirm) Then
Exit Sub
End If
If MsgBox("確認本筆進貨單?", vbYesNo, "進貨單確認") = vbNo Then
Exit Sub
End If
Dim strSQL As String
Dim rst As ADODB.Recordset
Dim Net As Integer
Me.Confirm = True
DoCmd.RunMacro "mrcSaveRecord"
If Me.Property = "1" Then
Net = 1
Else
Net = -1
End If
strSQL = "SELECT * " & _
"FROM PurchaseDetails " & _
"WHERE PurchaseID = '" & Form_進貨單維護![PurchaseID] & "' "
Set rst = New ADODB.Recordset
rst.Open strSQL, CurrentProject.Connection
Do While Not rst.EOF
Call UpdateStocks(rst![ProductID], Me![WarehouseID], rst![Quantity] * Net)
rst.MoveNext
Loop
End Sub
Code:
Public Sub UpdateStocks(ProductID As String, WarehouseID As String, Quantity As Long)
If ProductID = "" Or WarehouseID = "" Or Quntity = 0 Then
Exit Sub
End If
Dim strSQL As String
Dim rst As ADODB.Recordset
Dim cmd As ADODB.Command
Set rst = New ADODB.Recordset
Set cmd = New ADODB.Command
cmd.ActiveConnection = CurrentProject.Connection
strSQL = "SELECT ProductID " & _
"FROM Stocks " & _
"WHERE ProductID = '" & ProductID & "' " & _
"AND WarehouseID = '" & WarehouseID & "'"
rst.Open strSQL, CurrentProject.Connection
If rst.BOF Then
strSQL = "INSERT INTO Stocks " & _
"(ProductID, WarehouseID, Quantity) " & _
"VALUES (" & _
"'" & ProductID & "', " & _
"'" & WarehouseID & "', " & _
Quantity & ")"
cmd.CommandText = strSQL
cmd.Execute
Else
strSQL = "UPDATE Stocks " & _
"SET Quantity = Quantity + " & Quantity & " " & _
"WHERE ProductID = '" & ProductID & "' " & _
"AND WarehouseID = '" & WarehouseID & "'"
cmd.CommandText = strSQL
cmd.Execute
End If
rst.Close
Set rst = Nothing
Set cmd = Nothing
End Sub
Attachments
Last edited: