Skip to content
Advertisement

Adding new records to Access table on an update to a linked table in Access VBA SQL

I have two table one (skillsMatrix) the other table is (elementTree) with columns [mediumElement], [ID] in table skillsMatrix the mediumElement is a lookup dropdown of the mediumElements in table two. I want to write a macro to update skills matrix table to add a new record “name”, “new topic”,”” and not duplicate any of the other records when a new mediumElement is added to elementTree.

Table: skillsMatrix

id employee mediumElement completionDate
autoNumber Dave Walking 10/27/2020

Table: elementTree

Id mediumElement
26 Walking
27 Running

I’d like the skillsMatrix table to look like this after running the code

id employee mediumElement completionDate
autoNumber Dave Walking 10/27/2020
autoNumber Dave Running

I have tried the following to troubleshoot for building out the logic. The following prints out with RS always starting with 1 and ME starting with the proper ID for the mediumElement in element tree.
rs
1
ME
26
rs
2
ME
27
rs
3
ME
28
rs
4
ME
29
rs
5
ME
30
rs
6
ME
31
rs
7
ME
32
rs
8
ME
33
rs
9
ME
34
rs
10
ME
35

Nothing happens/wrong thing happens as the rs.Fields(“mediumElement”) does not give the what I would expect as the correct value. Instead of rs.[mediumElement] displaying the lookup ID of element from the elementTree table it always displays 1 through number of records in RS for rs.Fields(“mediumElement”). There is an employees table and the IDs are being saved in skillsMatrix. Although I used the lookup wizard when building the connections so that could be the problem. I apologize for my poor vernacular I’m pretty new to access and SQL.

I do not want all employees to be updated with the associated new element. The code is a sub controlled by a button press and the employee to be updated is selected on that form with the control employeeName

EDIT: On of the suggestion looking into Insert Select

the following works for adding the mediumElements to the skills matrix table, based on whether or not they exist for a user. Is there a way to also add the employee name to the skills matrixTable with the same Insert Into?

Advertisement

Answer

If employee is selected via a combobox on form, there is no need to open a recordset just to get employee ID. EmployeeID should be a hidden column of combobox and combobox should have that as its value.

idValue = Me.employeeName

If Employee ID is not available on form, a recordset is still not needed. Use DLookup.
idValue = DLookup("ID", "employeeTable", "[Employee Name]='" & Me.employeeName & "'")

Include employee field in INSERT clause and concatenate idValue to produce a calculated field in the SELECT from elementTree clause.

If employee and mediumElement are defined as a compound index in table, then don’t really need the WHERE criteria since duplicate pairs will not be allowed. I don’t know if this WHERE criteria slows or improves performance.

If new element ID can be captured from form, simplify code:

Use CurrentDb.Execute instead of DoCmd.RunSQL and won’t get warning popups.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement