You should be able to merge your AddData button with the Update button, so one button does all:
I don't know what code you've got behind your AddData button but I'm assuming it's along the lines of:
- DoCmd.RunCommand acCmdSaveRecord
-
DoCmd.GoToRecord , , acNewRec
(or similar - something that contains a save new record function)
You could change this to include an if statement based on a Dlookup:
- If IsNull(Dlookup("YourTableIDName", "TableLaptops", "[Quantity]=" & Me.Quantity & " AND " & "[Department]='" & Me.Department & "'" & " AND " & "[Location]=' & Me.Location & "'" & " AND " & "[Description]='" & Me.Description & "'")) then
-
-
DoCmd.RunCommand acCmdSaveRecord
-
DoCmd.GoToRecord , , acNewRec
-
(Or whatever your current AddData procedure is for saving new records)
-
-
Else
-
-
DoCmd.OpenQuery "YourUpdateQuery"
-
-
End If
(Take note of the additional single quotes round string based fields in the Dlookup criteria)
This code is written under the assumption that the input fields for Quantity, Department, Location and Description on the form are named the same as the table columns - if not, change the Me.<field> areas to the correct names.
You might also want to change your update query so that the WHERE clause checks that all fields are the same, instead of just the description:
- UPDATE TableLaptops SET TableLaptops.Quantity = TableLaptops.Quantity + Forms!OrderLaptops!QuantityField
-
WHERE (((TableLaptops.Quantity)=Forms!OrderLaptops!Quantity) AND ((TableLaptops.Department)=Forms!OrderLaptops!Department) AND ((TableLaptops.Location)=Forms!OrderLaptops!Location) AND ((TableLaptops.Description)=Forms!OrderLaptops!Description));
I think that should handle what you're after - that way when you click the AddData button, it'll decide if a record already exists for that combination of quantity/location etc, and just update it, or if it doesn't find an existing record, it'll add a new one.
Hope that helps.
Adam.