?
Solved

Comparing Access database tables

Posted on 2008-10-14
7
Medium Priority
?
395 Views
Last Modified: 2012-05-05
What we have is two databases on seperate CD's, within those databases are tables that we need to compare from one database CD to the other database CD.  The column headings match, we are trying to see the differences in the data in each individual field between the two database tables.  Are there any tools that may accomplish this I have tried to search for some and have actually tried ones out, but they were unsuccessfull.  Any help on this would be great thankyou
0
Comment
Question by:Pvteye
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 15

Expert Comment

by:dbase118
ID: 22712163
How many fields are you looking to compare from each table? Im sure someone will post some handy code for you to use, but I have done this on a relatively small scale using the conditional formatting features. This would assume you could get both tables into one file.  Example:

Table 1
Employee ID
First Name
Last Name
City
State

Table 2
Employee ID
First Name
Last Name
City
State

Make a query with Employee ID then pull each field from both tables. In essence you end up with a query that has the unique identifier and then both data items for each field. You can then go into the conditional formatting and write syntax like IF Table2. First Name is not equal to Table1. First Name turn it red. If you write that for each of your fields it will essentially highlight everywhere that the tables differ in data.

As I said if you are looking at comparing a lot of field this may not be a feasable option due to time, but it workss OK on a small scale

0
 

Expert Comment

by:culminIT
ID: 22712406
Are you looking for a way to automatically run through the tables and compare the data?  This process can be simplified if you can guarantee that the fieldnames match 100%, and there is always one common field that you expect the data to be the same (eg. the unique ID or code).
If this is the case, you can loop through the table definitions of one database.  Create a select that joins the two tables on the common field, then loop through the other fields and compare the two tables.  You will also need to do left and right joins where the records don't exist in one or the other.
0
 

Author Comment

by:Pvteye
ID: 22713909
There are 40 or so field headings and about 190,000 lines of data to be compared, not sure if an automated tool would be better for this or some sort of code for joining the tables and things like that?
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Accepted Solution

by:
culminIT earned 1500 total points
ID: 22728969
If its only the two tables you want to compare to each other, I would recommend firstly creating 2 outer join queries to check for non-existence on both sides:
SELECT Table1.* FROM Table1 LEFT JOIN Table2 ON Table1.CommonField = Table2.CommonField WHERE Table2.CommonField Is Null;
SELECT Table2.* FROM Table1 RIGHT JOIN Table2 ON Table1.CommonField = Table2.CommonField WHERE Table1.CommonField Is Null;
For differences on the matched inner join, you can do the following:
Create a new table (eg. tmp) with a field for the fieldname (FieldName), Value in table 1 (OldValue) and table 2 (NewValue).  Then you can try and use the code snippet I attached.
Hope that helps!  

Public Sub CompareData()
 
  Dim rstCompare As Recordset, rstDifferences As Recordset
  Dim iRecordNum As Long, sField As String
  Dim iFieldNum As Integer, sName As String
  Dim OldValue As Variant, NewValue As Variant
  
  On Error GoTo ErrHandler
  
  Set rstCompare = CurrentDb.OpenRecordset("SELECT * FROM Table1 INNER JOIN Table2 ON Table1.CommonField = Table2.CommonField;", dbOpenSnapshot)
  Set rstDifferences = CurrentDb.OpenRecordset("tmp", dbOpenDynaset)
 
    iRecordNum = 0
    With rstCompare
      If Not .EOF Then
        .MoveFirst
        While Not .EOF
          iRecordNum = iRecordNum + 1
          For iFieldNum = 0 To .Fields.Count - 1
            sField = .Fields(iFieldNum).Name
            'Change this length to match your first table name
            If Left(sField, 7) = "Table1." Then
              sName = Right(sField, Len(sField) - 7)
              ' Columns to ignore
              If sName <> "CommonField" Then
                OldValue = .Fields("Table2." & sName)
                NewValue = .Fields(sField)
                If IsNull(OldValue) Then OldValue = "Null"
                If IsNull(NewValue) Then NewValue = "Null"
                
                If OldValue <> NewValue Then
                  
                  rstDifferences.AddNew
                    rstDifferences!FieldName = sName
                    rstDifferences!OldValue = OldValue
                    rstDifferences!NewValue = NewValue
                  rstDifferences.Update
                End If
              End If
            End If
          Next
          .MoveNext
        Wend
      End If
    End With
    
    'Clean up recordsets
    rstCompare.Close
    rstDifferences.Close
    Set rstCompare = Nothing
    Set rstDifferences = Nothing
    
  On Error GoTo 0
  
  Exit Sub
 
ErrHandler:
  'Add some error handling
  Resume Next
 
End Sub

Open in new window

0
 
LVL 1

Expert Comment

by:NO_CARRIER
ID: 23105300
I'm sorry to bring up this PAQ's question, but I am also attempting to do the same and follow the same solution... I'm just not sure what is meant by:
"Create a new table (eg. tmp) with a field for the fieldname (FieldName), Value in table 1 (OldValue) and table 2 (NewValue).  Then you can try and use the code snippet I attached."

I have done both check queries (outer joins), and no queries have been returned.
0
 
LVL 1

Expert Comment

by:NO_CARRIER
ID: 23105805
Nevermind, I figured it out... just had to read the code to understand.. :)
Thanks for the solution.
0
 

Expert Comment

by:culminIT
ID: 23119093
No problem, glad it helped.
0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question