ACC2000: How to Automatically Subtract a Quantity Ordered from Your Inventory |
The
information in this article applies to:
Advanced: Requires expert coding,
interoperability, and multiuser skills.
This article applies to a Microsoft Access database (.mdb) and a Microsoft
Access project (.adp).
This article discusses two methods that you can
use to automatically reduce the available inventory (or units in stock) by the
quantity ordered. Method 1 shows you how to use an update query, and is for Jet
databases only. Method 2 shows you how to use recordset code, and is for both
Jet databases and Access projects).
NOTE: To use Method 1 with Access projects, use the following
information to get started:
Q235359 Implementing
Query-by-Form in an Access Project
The following two methods show you how you can
automatically reduce inventory by the amount ordered by a customer. In the
examples, each method adds a product named Chai to the customer's order.
Before you begin, open the Products table in the Northwind sample database. If
you have not modified the Northwind sample database, product ID #1 (that is,
Chai) has a value of 39 in the UnitsInStock field. After a customer places an
order for 9 items of Chai, the UnitsInStock field will automatically be reduced
to 30.
The examples affect only new orders, not changes to existing orders. If you
change the quantity of an existing order, the UnitsInStock field will not be
updated. In many cases, it is bad business practice to change an existing
order. Instead, if a customer requires more of the same product, you should
enter a new order.
These examples do not consider that customers may return orders. It is better
that you design an entirely new form to track and increment the units in stock
whenever a customer returns an order.
These examples make use of the existing Orders and Orders Subform form in the
Northwind database. The BeforeUpdate property of the Orders Subform form
already contains code. Do not modify the existing code in any way. Add the
sample code to the BeforeUpdate event of the Orders Subform form, but do not
replace the existing code. Inserted the code in the examples after the existing
End If statement and before the End Sub statement.
[Forms]![Orders]![Orders Subform].[Form]![ProductID]
[UnitsInStock]-[Forms]![Orders]![Orders Subform].[Form]![Quantity]
If Me.NewRecord Then
DoCmd.SetWarnings False
DoCmd.OpenQuery "qryUpdateUnitsInStock", acViewNormal, acEdit
DoCmd.SetWarnings True
End If
If Me.NewRecord Then
Dim dbs As DAO.Database
Dim rst As DAO.Recordset
Set dbs = CurrentDb
Set rst = dbs.OpenRecordset("Products", dbOpenTable)
rst.Index = "PrimaryKey"
rst.Seek "=", Me!ProductID
rst.Edit
rst("UnitsInStock") = rst("UnitsInStock") - Me!Quantity
rst.Update
rst.Close
Set rst = Nothing
Set dbs = Nothing
End If
If Me.NewRecord Then
Dim con As New ADODB.Connection
Dim rst As ADODB.Recordset
Set con = CurrentProject.Connection
Set rst = New ADODB.Recordset
rst.Open "Products", con, adOpenKeyset, adLockOptimistic
rst.Find "ProductID = " & Me!ProductID, 0, adSearchForward, 1
rst("UnitsInStock") = rst("UnitsInStock") - Me!Quantity
rst.Update
rst.Close
Set rst = Nothing
Set con = Nothing
End If