0

Completely new to Access VBA so please be nice!

I have tblNew and tblTeam.

I want to loop through each record in tblNew to check if it already exists in tblTeam. The identifier is the email address. If the record does not exist then I want to add the record to tblX.

I'm stuck on searching to check if the record exists and how to loop through each record in the tblNew.

Apologies if I've duplicated any questions, I really couldn't understand or find what I was looking for.

2
  • Are you searching to see if the entire record is in tblTeam, or just the unique identifier of EmailAddress? Commented Aug 22, 2018 at 21:06
  • Don't "Loop" through a table. That is an absolute last resort. Instead get a "Set" that matches your definition SELECT tblNew.emailAddress FROM tblNew LEFT OUTER JOIN tblTeam ON tblNew.emailAddress = tblTeam.EmailAddress WHERE tblTeam.EmailAddress IS NULL GROUP BY emailAddress that will give you a list of distinct email address in tblNew that isn't tblTeam already. Now stick INSERT INTO tblX (emailAddress) <that same SELECT statement> to insert them all at once. No loops and just on SQL statement will do this. Commented Aug 22, 2018 at 21:11

1 Answer 1

2

VBA is not necessarily required for this task - it can be quite easily achieved using an INSERT statement (aka Append Query), for example:

INSERT INTO tblX
SELECT 
    tblNew.*
FROM    
    tblNew LEFT JOIN tblTeam ON tblNew.EmailAddress = tblTeam.EmailAddress
WHERE 
    tblTeam.EmailAddress IS NULL
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.