Update an Access Table from Unbound Form Controls with VBA
Question details
Update an inventory record from unbound form controls and save the adjustment in a history table.

- Product
- Microsoft Access
- Device & OS
- not provided
- Scenario
- A button on an unbound form identifies an inventory record using several conditions, updates its quantity, and records the adjustment reason and date.
- Observed behavior
- A standalone UPDATE statement placed behind the button is not recognized as VBA, and the form needs code to write both the inventory change and its history.
Confirm the inventory table's key fields and data types, and create a history table with fields for the old quantity, new quantity, reason, and adjustment date. Check the form's control names and confirm that the Microsoft Office Access database engine Object Library is available under Tools > References in the VBA editor.
Run parameterized queries from the form button
Use DAO in the button's VBA event to update one matching inventory record and insert its history entry in the same transaction.
The example assumes Inventory_Table has ProductCode, WarehouseID, and a non-null whole-number Quantity. Inventory_Adjustment_Records has ProductCode, WarehouseID, OldQuantity, NewQuantity, AdjustmentReason, and AdjustmentDate. Replace these names and data types with those in your database.
A unique index on the fields that identify an inventory record helps ensure that the WHERE conditions match only one row. The example also checks the previous quantity when updating, so it can detect a conflicting change.
Open the form in Design View and use the Property Sheet to confirm the names of the product, warehouse, new quantity, and reason controls. The example uses txtProductCode, txtWarehouseID, txtNewQuantity, and txtReason.
Select the adjustment button in Design View. On the Property Sheet's Event tab, set On Click to [Event Procedure], then click the ellipsis button to open the VBA editor.
Replace the button's Click procedure with the following example, changing cmdAdjust and all table, field, control, and parameter types as needed: Private Sub cmdAdjust_Click() Dim db As DAO.Database Dim ws As DAO.Workspace Dim q As DAO.QueryDef Dim rs As DAO.Recordset Dim code As String, reason As String Dim warehouse As Long, oldQty As Long, newQty As Long Dim inTransaction As Boolean Dim errorMessage As String On Error GoTo HandleError code = Trim$(Nz(Me!txtProductCode.Value, "")) reason = Trim$(Nz(Me!txtReason.Value, "")) If code = "" Or reason = "" Or Not IsNumeric(Me!txtWarehouseID.Value) Or Not IsNumeric(Me!txtNewQuantity.Value) Then Err.Raise vbObjectError + 1, , "Enter a product, warehouse, whole-number quantity, and reason." End If If CDec(Me!txtWarehouseID.Value) <> Fix(CDec(Me!txtWarehouseID.Value)) Or CDec(Me!txtNewQuantity.Value) <> Fix(CDec(Me!txtNewQuantity.Value)) Then Err.Raise vbObjectError + 2, , "Warehouse ID and quantity must be whole numbers." End If warehouse = CLng(Me!txtWarehouseID.Value) newQty = CLng(Me!txtNewQuantity.Value) Set db = CurrentDb Set ws = DBEngine.Workspaces(0) ws.BeginTrans inTransaction = True Set q = db.CreateQueryDef("", "PARAMETERS pCode Text(50), pWarehouse Long; SELECT Quantity FROM Inventory_Table WHERE ProductCode = pCode AND WarehouseID = pWarehouse;") q.Parameters("pCode").Value = code q.Parameters("pWarehouse").Value = warehouse Set rs = q.OpenRecordset(dbOpenSnapshot) If rs.EOF Then Err.Raise vbObjectError + 3, , "No matching inventory record was found." oldQty = rs!Quantity rs.MoveNext If Not rs.EOF Then Err.Raise vbObjectError + 4, , "More than one inventory record matched." rs.Close Set rs = Nothing Set q = Nothing Set q = db.CreateQueryDef("", "PARAMETERS pNew Long, pCode Text(50), pWarehouse Long, pOld Long; UPDATE Inventory_Table SET Quantity = pNew WHERE ProductCode = pCode AND WarehouseID = pWarehouse AND Quantity = pOld;") q.Parameters("pNew").Value = newQty q.Parameters("pCode").Value = code q.Parameters("pWarehouse").Value = warehouse q.Parameters("pOld").Value = oldQty q.Execute dbFailOnError If q.RecordsAffected <> 1 Then Err.Raise vbObjectError + 5, , "The inventory record changed or could not be updated." Set q = Nothing Set q = db.CreateQueryDef("", "PARAMETERS pCode Text(50), pWarehouse Long, pOld Long, pNew Long, pReason Text(255), pDate DateTime; INSERT INTO Inventory_Adjustment_Records (ProductCode, WarehouseID, OldQuantity, NewQuantity, AdjustmentReason, AdjustmentDate) VALUES (pCode, pWarehouse, pOld, pNew, pReason, pDate);") q.Parameters("pCode").Value = code q.Parameters("pWarehouse").Value = warehouse q.Parameters("pOld").Value = oldQty q.Parameters("pNew").Value = newQty q.Parameters("pReason").Value = reason q.Parameters("pDate").Value = Now() q.Execute dbFailOnError ws.CommitTrans inTransaction = False MsgBox "Inventory adjustment saved.", vbInformation Exit Sub HandleError: errorMessage = Err.Description On Error Resume Next If Not rs Is Nothing Then rs.Close If inTransaction Then ws.Rollback On Error GoTo 0 MsgBox "Adjustment not saved: " & errorMessage, vbExclamation End Sub
Return to Form View, enter values that identify one inventory record, and click the adjustment button. Open Inventory_Table and Inventory_Adjustment_Records to verify the new quantity and the corresponding old and new values, reason, and date.

Consider a bound form for simpler record editing
Use a bound form when users can edit inventory records directly and an unbound workflow is not required.
Use WPS Office for Inventory Spreadsheets and Reports
WPS Office is a free, lightweight alternative for everyday office work. Its familiar interface and Microsoft Office format support make it easy to move inventory spreadsheets and reports between suites. It does not run Access forms or fix Access VBA.

Frequently Asked Questions
Why does Access say the UPDATE command is not defined?
UPDATE is an SQL statement, not a standalone VBA command. In the button's On Click event, create a DAO QueryDef containing the SQL and run it with Execute.
How do I make sure the history entry is saved with the update?
Use a DAO workspace transaction: call BeginTrans before reading and updating the record, insert the history row, and call CommitTrans only after both writes succeed. Call Rollback in the error handler if either write fails.
How can I prevent the wrong inventory record from being updated?
Include all identifying fields in the WHERE clause, enforce an appropriate unique index in the table, and check that the update query's RecordsAffected value is exactly 1.
Do unbound controls save their values automatically?
No. Unbound controls do not write to a table automatically. Use a button event and a query or recordset to save their values, or set a form's Record Source and controls' Control Source properties to use bound editing.




