Compare two tables in MS Access


I have 3 tables in my project:


A Batch is entered into the tblBatches table and then individual jobs associated with the Batch is entered into tblReferenceData.  They are both joined accordingly.  When the job gets completed all the data associated with the job gets automatically added to the tblJobData table (via an external source)

To ensure the Batch has been completed, I need to check the data in the tblJobData table against the data in tblReferenceData to check that there is a corresponding entry in the tblJobData table for each entry in tblReferenceData for the Batch reference.

If there is a record in tblReferenceData but there isn't a corresponding entry (with the same REF ID) in tblJobData the user needs to be informed of what Job is missing.

Hope I have explained this ok.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jeffrey CoachmanMIS LiasonCommented:
Not sure of your design there...(without seeing the associated fields...)
For example, is not clear how tblJobData is related to the other two tables.

Most techniques to do this revolve around simply creating a "Completed" (yes/no) calculated field in a query.
This avoids all the complex "compare" code needed to validate across tables.
It also eliminates the need for the second table altogether.
In this design, if the number of start an end dates are the same, the task is "Complete"

For example:
jID (PK)
jComplete (calculated in a query)

jdID (PK)
jd_jID (FK)

A very basic sample is attached...


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Jeffrey CoachmanMIS LiasonCommented: sample modified a bit to show "Missing", instead of blank dates
anthonytrAuthor Commented:
Hi Jeff,

Thanks for the attached examples.  I'll show my code which I'm currently working on when I get into the office tomorrow.  I have to use a separate table to store the JobData (tblJobData) as this data is supplied to the database from an external source which is automated (in simple terms, it dumps the data into the said table).  By using the data which it dumps I can then see if a certain job has been completed (if the tblJobData has JobID = 10 then Job ID = 10 which was created in tblReferenceData must have been completed.

I started to write a simple 'show unmatched data' query but I think I have confused the WHERE clause somewhat.  I'll post tomorrow with an update.

Thank you for your time - very much appreciated.
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

Jeffrey CoachmanMIS LiasonCommented:

There are many ways to approach this.

A sample db would be better.
anthonytrAuthor Commented:

As promised here is the code I am using:

Private Sub JobComplete_Click()
Dim db As DAO.Database, rs As DAO.Recordset, strSQL As String
Set db = CurrentDb
'Define SQL Query
strSQL = "SELECT, qryReferenceData.customer_id, qryReferenceData.batch_number, qryReferenceData.tbl_batches.batch_reference, qryReferenceData.batch_output_location, qryReferenceData.create_date,, qryReferenceData.tbl_reference_data.batch_reference, qryReferenceData.reference_1, qryReferenceData.reference_2, qryReferenceData.reference_3, qryReferenceData.reference_4, qryReferenceData.reference_5 FROM qryReferenceData LEFT JOIN tbl_scan_data ON qryReferenceData.[tbl_reference_data].[id] = tbl_scan_data.[Reference ID] WHERE (((tbl_scan_data.[Reference ID]) Is Null)) AND tbl_scan_data.[Batch ID] =;"

Set rs = db.OpenRecordset(strSQL)
'Return Result in Loop until end of results
Do While Not rs.EOF
'Insert code here to list elements
'We will loop through and popup a message box
'for this example

MsgBox "Job: " & rs.Fields(0) & " " & rs.Fields(1) & ": " & rs.Fields(2)
'This would pop a message box for each loop
'and display each Job called from the
'SQL query

'move to the next record for the next loop
'Close the Database
Set db = Nothing
Set rs = Nothing
End Sub

Open in new window

I think the:

WHERE (((tbl_scan_data.[Reference ID]) Is Null)) AND tbl_scan_data.[Batch ID] =;"

is incorrect.

I need to add the filter on BatchID as well.  The ID is on the form where the button is pressed (

anthonytrAuthor Commented:
Just thinking over this again.  

Would it be better to have the code look for unmatched records and then notify the user if there are any and then allow the user to print out a report of the unmatched records instead of having them displayed in a MsgBox?

The MsgBox could read:

"There are X number of unmatched items.  Do you wish to print a report of these Jobs?"
Jeffrey CoachmanMIS LiasonCommented:
I am getting confused.

So are you saying that the code works,
...and now you just want a custom message, then an option to print the results (the unmatched records)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.