Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Comparing two database tables

Status
Not open for further replies.
May 21, 2003
64
US
I have and Access Database I use for Job Requests. There are two main tables. A requestor enters data in a form that ties to the 30 different fields in the first table. The requests often change midstream, so I have an update button tied to a macro which appends the old data prior to an update to a second table with the same fields. Because there are 30 fields, I need an efficient way to detemine which of the field(s) changed. Ideally I would like to highlight changes (differences between the old and new table) in red so they would be easily identified. The first step however is comparing the 30 fields and isolating the differences. Any thoughts on a good way to do this? Thanks. D
 
How about looping through each record and comparing the fields?

Code:
'Typed, not tested
'Numeric ID
Set rsA=CurrentDB.OpenRecordset("Select * From tblA Where ID=" _
& Me.ID)
Set rsB=CurrentDB.OpenRecordset("Select * From tblB Where ID=" _
& Me.ID)

For Each fld In rsA.Fields
    If fld <> rsB.Fields(fld.Name) Then
       Me(fld.Name).Backcolor=vbRed
    End If
Next

Or there abouts.
 
I'm not too savy with coding but i do see what you are doing . Do I need to declare rsa and rsb? I reworked things a bit too. I'm comparing the table against a query that pulls data from table b. This table also functions as a change log, so making it a query will allow it to only compare the most recent changes. Do I just select * from queryname in the second step? How is this code called? From the form? Ideally after the update button is clicked the code would execute. How do I do this if the onclick command is already executing an append macro? Thanks. D
 
You can declare rsA and rsB as DAO.Recordsets:

Dim rsA As DAO.Recordset

Make sure you have a reference to the Microsoft DAO 3.x Object Library.

You can add the code after the append macro, if you wish. It does not update anything, but it is still wise to backup, as altering forms can sometimes cause problems.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top