Please disregard this query. It was only the unique constraint making it seem
to work for me. Sorry.
Hopefully this will work. There is a subprogram, which will create two tables,
People and People2. People has 3 rows, and People2 has 2 rows, one of which
duplicates a row in People. Note that neither table has a primary key nor a
unique index.
Query1 inserts a new row into People. Query2 inserts only one new row into
People, because the other row would be a duplicate.
Sub CreateTables()
With CurrentProject.Connection
..Execute _
" CREATE TABLE People (" & _
" first_name VARCHAR (20)," & _
" last_name VARCHAR (20)," & _
" birth_date DATETIME," & _
" zip_code CHAR(5));"
..Execute "INSERT INTO People VALUES ('James', 'Nelson', #6/6/1962#, '47588');"
..Execute "INSERT INTO People VALUES ('Harvey', 'Little', #11/19/1968#,'47588');"
..Execute "INSERT INTO People VALUES ('Mary', 'Greer', #5/2/1973#,'47519');"
..Execute _
" CREATE TABLE People2 (" & _
" first_name VARCHAR (20)," & _
" last_name VARCHAR (20)," & _
" birth_date DATETIME," & _
" zip_code CHAR(5));"
..Execute "INSERT INTO People2 VALUES ('Philip', 'Schulz', #4/10/1978#,'47519');"
..Execute "INSERT INTO People2 VALUES ('Mary', 'Greer', #5/2/1973#,'47519');"
..Execute "CREATE TABLE Dummy (dummy_id INT NOT NULL PRIMARY KEY);"
..Execute "INSERT INTO Dummy VALUES (1);"
End With
End Sub
Query1: (inserts literal values into People table)
INSERT INTO People (first_name, last_name, birth_date, zip_code)
SELECT "Ralph", "Schmitt", "12/12/1979", "47518"
FROM Dummy
WHERE NOT EXISTS (SELECT *
FROM People
WHERE first_name = "Ralph"
AND last_name = "Schmitt"
AND birth_date =#12/12/1979#
AND zip_code = "47518");
Query2: (inserts values from People2 into People table)
INSERT INTO People ( first_name, last_name, birth_date, zip_code )
SELECT People2.first_name, People2.last_name, People2.birth_date,
People2.zip_code
FROM People2
WHERE NOT EXISTS (SELECT *
FROM People
WHERE People.first_name = People2.first_name
AND People.last_name = People2.last_name
AND People.birth_date = People2.birth_date
AND People.zip_code = People2.zip_code);