Link to home
Create AccountLog in
Avatar of anthonytr
anthonytrFlag for United Kingdom of Great Britain and Northern Ireland

asked on

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.
Avatar of Jeffrey Coachman
Jeffrey Coachman
Flag of United States of America image

Link to home
Create an account to see this answer
Signing up is free. No credit card required.
Create Account sample modified a bit to show "Missing", instead of blank dates
Avatar of anthonytr


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.

There are many ways to approach this.

A sample db would be better.

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 (

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?"
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)