I am trying to build a function that updates the units in stock in the table Products based on a specified
OrderID in the table Order Details.I have obtained from a friend a function very close to my needs which runs as follows:
Public Function UpdateStock()
Dim db As Database
Dim strSQL As String
Set db = CurrentDb
strSQL = “UPDATE Products RIGHT JOIN [Order Details] ON products.Productid = “” _
& “[Order Details].ProductID SET Products.UnitsInStock = ” _
& “[Quantity]+[UnitsInStock] ” _
& “WHERE ([Order Details].OrderID)= ” & MyOrder & “;”
DoCmd.RunSQL strSQL
However he doesnt know what MyOrder in the above code may mean. If it is the number of the order,can i change it with a StrWhere pointing to a specific order number to choose from the list?Also, can somebody help me in applying this to my needs?
I have a form called frmCustomerOrders,and in it there is a ListBox called OrdersList. In this list the orders are enumerated according to their orderid.
What i want is with one click to update the selected order.
Also,in the sql what is better to use, right join or inner join?
Just for information i want to inform what i have tried with no success, becasue i received Run Time error 3079
The specified field “orderid” could refer to more than one table listed in the FROM clause in your SQL statement
Public Function UpdateStock()
Dim MyForm As Form
Set MyForm = Forms![frmCustomerOrders]
Dim strWhere As String, strCondition As String
strCondition = “OrderID=” & MyForm.OrderID
strWhere = ” WHERE ” & strCondition
Dim strSQL As String
strSQL = “UPDATE orders INNER JOIN (products INNER JOIN ” & ” [order details] ON (products.Productid = [order details].ProductID) ” & “AND
(products.Productid = [order details].ProductID)) ” & “ON orders.orderid = [order details].OrderID” & _
” SET ” & “products.UnitsOnStock = products.UnitsOnStock +[order details].cartons” & strWhere
CurrentDb.Execute strSQL
End Function
My underlying query ofr the list is the following:
SELECT orders.orderid, orders.orderdate, customers.CompanyName, [order details].cartons
FROM (affiliates INNER JOIN (customers INNER JOIN orders ON (customers.Customerid = orders.customerid) AND (customers.Customerid = orders.customerid))
ON affiliates.afid = customers.afid) INNER JOIN [order details] ON orders.orderid = [order details].OrderID
ORDER BY orders.orderdate;