I am accessing a query through VBA and am having an odd response from an append query. I am appending a record to an ALERT table if there is either a non-existing employee name, a null ID, or a duplicate ID. I am doing this process through three individual append queries that are indicated below:
Code:
'Append New Employee to Alert Table
StrSQL = "INSERT INTO Alerts ( PERIOD, [Id], [Transaction Source], Yes, [Hold], " & _
"[Name], Project, Task, [Type], [Date], [Employee Name], Quantity, [Rate], " & _
"UOM, [Cost], [field 17], [Amount], Comment, Field19, Bill, Alert ) SELECT " & _
"Temp.PERIOD, Temp.[Id], Temp.[Transaction Source], Temp.Yes, Temp.Hold, " & _
"Temp.[Name], Temp.Project, Temp.Task, Temp.[Type], Temp.[Date], " & _
"Temp.[Employee Name], Temp.Quantity, Temp.[Rate], Temp.UOM, Temp.[Cost], " & _
"Temp.[17], Temp.[Amount], Temp.Comment, Temp.Empty, Temp.Bill, 'New Name' AS [Alert] " & _
"FROM Temp LEFT JOIN NameCard ON Temp.[Employee Name] = NameCard.Name WHERE (((NameCard.Name) Is Null));"
Code:
'Append Duplicate ID to Alert Table
StrSQL = "INSERT INTO Alerts ( PERIOD, [Id], [Transaction Source], Yes, [Hold], " & _
"[Name], Project, Task, [Type], [Date], [Employee Name], Quantity, [Rate], " & _
"UOM, [Cost], [field 17], [Amount], Comment, Field19, Bill, Alert ) SELECT " & _
"Temp.PERIOD, Temp.[Id], Temp.[Transaction Source], Temp.Yes, Temp.Hold, " & _
"Temp.[Name], Temp.Project, Temp.Task, Temp.[Type], Temp.[Date], " & _
"Temp.[Employee Name], Temp.Quantity, Temp.[Rate], Temp.UOM, Temp.[Cost], " & _
"Temp.[17], Temp.[Amount], Temp.Comment, Temp.Empty, Temp.Bill, 'Duplicate ID' AS [Alert] " & _
"FROM Temp INNER JOIN MASTER ON Temp.[ID] = MASTER.ID;"
Code:
'Append Null ID to Alert Table
StrSQL = "INSERT INTO Alerts ( PERIOD, [Id], [Transaction Source], Yes, [Hold], " & _
"[Name], Project, Task, [Type], [Date], [Employee Name], Quantity, [Rate], " & _
"UOM, [Cost], [field 17], [Amount], Comment, Field19, Bill, Alert ) SELECT " & _
"Temp.PERIOD, Temp.[Id], Temp.[Transaction Source], Temp.Yes, Temp.Hold, " & _
"Temp.[Name], Temp.Project, Temp.Task, Temp.[Type], Temp.[Date], " & _
"Temp.[Employee Name], Temp.Quantity, Temp.[Rate], Temp.UOM, Temp.[Cost], " & _
"Temp.[17], Temp.[Amount], Temp.Comment, Temp.Empty, Temp.Bill, 'Null ID' AS [Alert] " & _
"FROM Temp WHERE ((Temp.[ID]) = Null));"
When I run these queries, I get multiple copies of the different examples that I run. I have included fake data (showing only 2 rows for sake of displaying important data):TEMP Table
ID
|
Employee Name
|
12345 |
Tony Jones |
5678 |
New Employee Name |
|
Bob Smith |
Where in the example data, ID 12345 is a duplicate ID, 5678 is a new employee, and Bob Smith is a null ID. The data I get is similar to below:ALERTS Table
ID
|
Employee Name
|
Alert
|
12345 |
Tony Jones |
Duplicate ID |
5678 |
New Employee Name |
New Name |
|
Bob Smith |
Null ID |
12345 |
Tony Jones |
Duplicate ID |
5678 |
New Employee Name |
New Name |
|
Bob Smith |
Null ID |
12345 |
Tony Jones |
Duplicate ID |
5678 |
New Employee Name |
New Name |
|
Bob Smith |
Null ID |
Can anyone help guide me as to why these queries, ran only once, would produce 3+ copies of the data I need?
Thank you for your help!