Insert record into table if entry does not exist in another table- with an extra twist
Will this do? Obviously add some square brackets and stuff. Not too into Access myself.
INSERT INTO table2 (file_index, celeb_name)
SELECT file_index, 'Audrey Hepburn'
FROM table1
WHERE filename = 'aud'
AND file_index NOT IN (SELECT DISTINCT file_index
FROM table2
WHERE celeb_name = 'Audrey Hepburn')
As I said in comments, NOT IN is not well-optimized by Jet/ACE and it's usually more efficient to use an OUTER JOIN. In this case, because you need to filter on the outer side of the join, you'll need a subquery:
INSERT INTO photos_by_celebrity ( ORIG_FILE_INDEX, celebrity_name )
SELECT tblOriginal_Files.ORIG_FILE_INDEX, 'Audrey Hepburn'
FROM tblOriginal_Files
LEFT JOIN (SELECT DISTINCT ORIG_FILE_INDEX
FROM photos_by_celebrity
WHERE celebrity_name = 'Audrey Hepburn') AS Photos
ON tblOriginal_Files.ORIG_FILE_INDEX = Photos.ORIG_FILE_INDEX
WHERE Photos.ORIG_FILE_INDEX Is Null;
(that may not be exactly right -- I'm terrible with writing SQL by hand, particularly getting the JOIN syntax right)
I must say, though, that I'm wondering if this will insert too many records (and the same reservation applies to the NOT IN version).