Quote:
Originally Posted by jaxjagfan
Let's assume you have a frmInvoice with cboProdID (Combobox for selecting ProductID), txtQty (Textbox for entering Qty) and txtAvgCost (for displaying average cost). You pick the ProductID and you enter a Qty. There's 2 things that need to happen now. Your form needs to look up the ProductID, Qty, and Cost price for each supplier - it is actually the maximum qty that is less than the value you entered and the minumum price. And then it needs to get an average from that list.
I created a query for part 1 - qryAvg1
You can create the form I mention here or change the references to your own form. Create a new query in design view. Don't select any tables or queries and close the dialog. Click on the "SQL" button on the toolbar and paste the following SQL and save the query as qryAvg1:
SELECT Max(tblcostPrice.Qty) AS Qty, tblcostPrice.Supplier, Min(tblcostPrice.[Cost Price]) AS Price
FROM tblcostPrice
WHERE (((tblcostPrice.ProductID)=[Forms]![frmInvoice]![cboProdID]) AND ((tblcostPrice.Qty)<=[Forms]![frmInvoice]![txtQty]))
GROUP BY tblcostPrice.Supplier;
Part 2:
On the AfterUpdate property of txtQty select [Event Procedure] from the drop list and then click on (...) next to it to open code window. Paste the following:
-
-
Me.txtAvgCost = DAvg("[Price]", "qryAvg1")
-
-
If you add the same line to the AfterUpdate property of the cboProdID it will enable you to change which product you are invoicing and not have to reselect a qty.
Save the changes and open the form and enter some selections.
Thank you for your prompt reply, thank so much for the coding, otherwise I am still scratching my head. but when I try some calculation some result ok, some not ok, here an sample
BBB SUPPLER...1000.....0.5511
..........................5000.....0.4411
.........................12000....0.3311
CCC SUPPLIER..1000.....0.5513
.........................5000.....0.4413
.......................11000....0.3313
DDD SUPPLIER..1000.....0.5521
..........................5000.....0.4421
........................11000....0.3312
If I order 11,000, manual calculation is
5000.............0.4411.........2205.5
11000...........0.3313.........3644.3
11000.......... 0.3312.........3643.2
.....................................9493.00
average cost = 0.3516 (9493/27000)
But base on your coding I get 0.3679