Deduct food recipes from the main inventroy?

Attachment Size
database_tables.jpg 55.89 KB
Hi all! This is my scenario where I need help: I purchase food raw materials (such as Rice, Oil, Salt, Sugar, Soda, Coffee, Maize etc), I record this in the inventory table I record to the system (table menu), types of foods I will be making (we call it menu), menu can be something like (Rice with Fish, Salad, Coffee, Soda, Pizza etc) I also record to the system (table recipe) what raw materials makes up each of the menu I specified above, and what amount of the raw material. (look like this Rice & Fish :20units of Rice, 1 fish, 2 units of salt), this for every other menu/food listed in paragraph 2 above. When I sell the food/menu, I want the appropriate amount of its recipes to be subtracted from the total that is available in the main Inventory table. So, if I sold 1 Rice&Fish, I want the system to subtract 20units of rice, 1fish,2Units of salt from the inventory table. (This is where I get into trouble) In my trying, I decided to create three tables (Inventory,Menu & Recipe), I am attaching a snapshot of how I designed the tables, please have a look. (Please note that in table recipe, menuID & InvID are foreign key) Now, FROM THE TABLES (see attached) If I sell (Click) “Salad” I will run the querry
  1. "SELECT InvID,quantity FROM Recipe WHERE menuID = '" & Me.ComboBoxMenu.Text & "'" '(selected here in this combobox is e.g. "Salad")
(This give me: Water (20), Salt (30)), according to the values I had specified in the other tables. Now my problem is, how can I deduct this (Water (20), Salt (30)) from their respective amounts in the Inventory table? How can I read what is in the inventory table and subtract the two-column results of a query? Can looping be used to do this? This confuses me and I would appreciate some help. *Sorry if I did not explain myself clearly, and thanks for continued support. Database: mysql.
Submitted byadminon Fri, 06/21/2013 - 18:17

Hi, That's seems so simple to do using an update query. Example:
  1. UPDATE Inventory INNER JOIN Recipe ON Inventory.InvID = Recipe.InvID SET Inventory.Amount = [Inventory].[Amount]-[Recipe].[Quantiy]
  2. WHERE (((Recipe.MenuID)="Salad"));
This will deduct whatever quantity you have in the recipe table based on the menu you sold.

that's correct use like this also  UPDATE I  SET I.AMOUNT=(I.AMOUNT-R.QUANTITY) FROM INVENTORY AS I INNER JOIN RECIPE R ON R.INVID=I.INVID INNER JOIN MENU M ON M.MENUID=R.MENU_ID WHERE M.MENUID='SALAD'

Add new comment