I was trying to Update a query on MS-Access. Let's Assume, I have two Tables (Employee, Department). I have (ID, Name, FK_DepartmentID) in Employee Table. And, two Column in Department(ID, DepartmentName) Table, Where I want to check if department Name exists in Department Table, then Update in Employee.FK_DepartmentID, else Null. Currently, I am updating with
UPDATE Employee, Department SET
Employee.Name = 'David',
Employee.FK_DepartmentID = Department.ID
WHERE (((Employee.ID)='55') AND ((Department.DepartmentName)='HR');
So this is updating my Employee Table. But Let's say User input Department.DepartmentName='IT', where 'IT' doesn't exist in Department Table (Or User input nothing). With the current query, it executes but doesn't update any row. But I want it to update Employee.Name alongside Employee.DepartmentID = Null. What can I do so I can reach what I want.
Really appreciate your time and thanks in advance.
What you are describing is an upsert, which is short for update or insert based on a select. you can find plenty more examples using the keywords ACCESS and Upsert. Access doesn't have special syntax for upserts. You have to write the select, update, insert, and logic yourself. I haven't found a way to get around using VBA for upserts in Access as you have to call a select query then choose either to update or insert. My example ended up being with strings but it is otherwise similar to what I think you are trying to do. So assuming a one to many relationship between departments and employees:
Here is the code be careful to notice that the strings are enclosed in ' ':