**The problem has been resolved and the program can now be executed.
I changed the names of the three parameters in the "UpdateStocks" public sub in th public function module. (they originally had the same names as the three columns in the "Stocks" table).** Thank you very much!
I have the purchase order and the purchase order subform.
There is a confirmation button on the purchase order, the "Confirm" button click event will call the "UpdateStocks" public function in the "Public Function" module.
Once the purchase order is confirmed, the "WarehouseID" of the purchase order, and the "ProductID" and "Quantity" of the purchase order subform, add or update the "ProductID" and "WarehouseID" and "Quantity" of the "Stocks Table".
The confirm button click event is as follows:
The public sub "UpdateStocks" in the public function module is as follows:
I changed the names of the three parameters in the "UpdateStocks" public sub in th public function module. (they originally had the same names as the three columns in the "Stocks" table).** Thank you very much!
I have the purchase order and the purchase order subform.
There is a confirmation button on the purchase order, the "Confirm" button click event will call the "UpdateStocks" public function in the "Public Function" module.
Once the purchase order is confirmed, the "WarehouseID" of the purchase order, and the "ProductID" and "Quantity" of the purchase order subform, add or update the "ProductID" and "WarehouseID" and "Quantity" of the "Stocks Table".
The confirm button click event is as follows:
Code:
Private Sub cmdConfirm_Click()
If Me.NewRecord Or Me.Dirty Or _
Me.PurchaseOrderSubform.Form.NewRecord Or Me.PurchaseOrderSubform.Form.Dirty Then
Exit Sub
End If
If Me.Confirm = True Or IsNull(Me.Confirm) Then
Exit Sub
End If
If MsgBox("Are you sure to confirm this order?", vbYesNo, "Order Confirm") = 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 = '" & Me.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
The public sub "UpdateStocks" in the public function module is as follows:
Code:
Public Sub UpdateStocks(ProdID As String, WHID As String, Qty As Long)
If ProdID = "" Or WHID = "" Or Qty = 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 = '" & ProdID & "' " & _
"AND WarehouseID = '" & WHID & "'"
rst.Open strSQL, CurrentProject.Connection
If rst.BOF Then
strSQL = "INSERT INTO Stocks " & _
"(ProductID, WarehouseID, Quantity) " & _
"VALUES (" & _
"'" & ProdID & "', " & _
"'" & WHID & "', " & _
Qty & ")"
cmd.CommandText = strSQL
cmd.Execute
Else
strSQL = "UPDATE Stocks " & _
"SET Quantity = Quantity + " & Qty & " " & _
"WHERE ProductID = '" & ProdID & "' " & _
"AND WarehouseID = '" & WHID & "'"
cmd.CommandText = strSQL
cmd.Execute
End If
rst.Close
Set rst = Nothing
Set cmd = Nothing
End Sub
rst.Close
Set rst = Nothing
Set cmd = Nothing
End Sub
Attachments
Last edited: