473,416 Members | 1,561 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,416 software developers and data experts.

Comparing Two Identical Tables - Loop Through fields in a Recordset

Forgive me for asking this question, I've trawled through nearly every
available post on this subject that I can find for a few weeks now but
nothing quite points me in the right direction.

I'm quite new to trying to mess around with VB and ADO within MS
Access and have realised the steep learning curve I have, but, I want
to try and solve this problem quickly and was wondering if anyone
would help me out??

I want to be able to compare two tables within the same .mbd and write
out any mismatched fields to a third table. From what I can gather
then this is best done via ADO and looping through recordsets of the
two tables and writing to a recordset of the third table? The two
tables are revisions of the same query written to different tables, so
the structure is exactly the same.

Table 1 and 2 have :

Field1, Field2, Field3, Field4, Field5, Field6, Field7

Where Field 1 is the Primary Key for the both tables.

I'd like to loop through each record comparing each field in Table1 to
it's corresponding field in Table2 where Table1.Field1 = Table2.Field1
and write out where there is a difference to a third table, where the
two fields are matching (no change), I'd like to put a null value; I
could then report on Table 3

So for example, if one record in each table looked like so:

Table1
Field1 = A
Field2 = B
Field3 = C
Field4 = D
Field5 = E
Field6 = F
Field7 = G

Table2
Field1 = A
Field2 = B
Field3 = CC
Field4 = D
Field5 = EE
Field6 = F
Field7 = GG

Then the resulting Table 3 would look like this

Field1 = A
Field2 = '' (Null)
Field3 = CC
Field4 = '' (Null)
Field5 = EE
Field6 = '' (Null)
Field7 = GG

Can anyone help out a damsel in distress? I'm using MS Access 2003

Thanks

Gill xx
Feb 20 '08 #1
4 6369
DFS
gi*************@googlemail.com wrote:
Forgive me for asking this question, I've trawled through nearly every
available post on this subject that I can find for a few weeks now but
nothing quite points me in the right direction.

I'm quite new to trying to mess around with VB and ADO within MS
Access and have realised the steep learning curve I have, but, I want
to try and solve this problem quickly and was wondering if anyone
would help me out??

I want to be able to compare two tables within the same .mbd and write
out any mismatched fields to a third table. From what I can gather
then this is best done via ADO and looping through recordsets of the
two tables and writing to a recordset of the third table? The two
tables are revisions of the same query written to different tables, so
the structure is exactly the same.

Table 1 and 2 have :

Field1, Field2, Field3, Field4, Field5, Field6, Field7

Where Field 1 is the Primary Key for the both tables.

I'd like to loop through each record comparing each field in Table1 to
it's corresponding field in Table2 where Table1.Field1 = Table2.Field1
and write out where there is a difference to a third table, where the
two fields are matching (no change), I'd like to put a null value; I
could then report on Table 3

So for example, if one record in each table looked like so:

Table1
Field1 = A
Field2 = B
Field3 = C
Field4 = D
Field5 = E
Field6 = F
Field7 = G

Table2
Field1 = A
Field2 = B
Field3 = CC
Field4 = D
Field5 = EE
Field6 = F
Field7 = GG

Then the resulting Table 3 would look like this

Field1 = A
Field2 = '' (Null)
Field3 = CC
Field4 = '' (Null)
Field5 = EE
Field6 = '' (Null)
Field7 = GG

Can anyone help out a damsel in distress? I'm using MS Access 2003

Thanks

Gill xx

Build a query that compares each column in the two tables, and joins on the
common ID column. This code won't be perfect but will help get you started.

dim db as database, rs as recordset, cSQL as string
set db = currentdb()

cSQL = "SELECT T1.ID, "

Set rs = db.OpenRecordset("JanData")
For i = 1 To rs.Fields.Count - 1
'COMPARE NUMERIC DATA - ABSOLUTE % CHANGE
cSQL = cSQL & "Format(Abs(T1.[" & rs(i).Name & "] - T2.[" & rs(i).Name &
"])/T2.[" & rs(i).Name & "],'0.00%') AS " & rs(i).Name & ", "

'COMPARE TEXT DATA
cSQL = cSQL & "IIf(T1.[" & rs(i).Name & "] <T2.[" & rs(i).Name &
"],'Diff','Match') AS " & rs(i).Name & ", "

Next i
rs.Close
Set rs = Nothing
cSQL = Trim(cSQL)
cSQL = Left(cSQL, Len(cSQL) - 1)

cSQL = cSQL & " FROM JanData T1 INNER JOIN FebData T2 ON T1.ID = T2.ID "

debug.print cSQL

Then cut and paste from the Immediate Window into a query window and run it.

Feb 20 '08 #2
gi*************@googlemail.com wrote:
Forgive me for asking this question, I've trawled through nearly every
available post on this subject that I can find for a few weeks now but
nothing quite points me in the right direction.

I'm quite new to trying to mess around with VB and ADO within MS
Access and have realised the steep learning curve I have, but, I want
to try and solve this problem quickly and was wondering if anyone
would help me out??

I want to be able to compare two tables within the same .mbd and write
out any mismatched fields to a third table. From what I can gather
then this is best done via ADO and looping through recordsets of the
two tables and writing to a recordset of the third table? The two
tables are revisions of the same query written to different tables, so
the structure is exactly the same.

Table 1 and 2 have :

Field1, Field2, Field3, Field4, Field5, Field6, Field7

Where Field 1 is the Primary Key for the both tables.

I'd like to loop through each record comparing each field in Table1 to
it's corresponding field in Table2 where Table1.Field1 = Table2.Field1
and write out where there is a difference to a third table, where the
two fields are matching (no change), I'd like to put a null value; I
could then report on Table 3

So for example, if one record in each table looked like so:

Table1
Field1 = A
Field2 = B
Field3 = C
Field4 = D
Field5 = E
Field6 = F
Field7 = G

Table2
Field1 = A
Field2 = B
Field3 = CC
Field4 = D
Field5 = EE
Field6 = F
Field7 = GG

Then the resulting Table 3 would look like this

Field1 = A
Field2 = '' (Null)
Field3 = CC
Field4 = '' (Null)
Field5 = EE
Field6 = '' (Null)
Field7 = GG

Can anyone help out a damsel in distress? I'm using MS Access 2003

Thanks

Gill xx
I might consider creating an append or maketable query. Add your two
tables to it and make a linkline between the two Field1's. Then create
a series of IIF() statements. Ex:
F2 : IIF(NZ(Table1!Field2,"") = NZ(Table2!Field2,""),Table1!Field2,Null)

Do the same for the rest of the fields.

Now run it. If acceptable, make the query type Append to append to
table3 or MakeTable to create table3.

Party
http://www.youtube.com/watch?v=aUUGblNjK20

Feb 20 '08 #3
On 21 Feb, 11:13, gillianbrook...@googlemail.com wrote:
Lyle, thank you for this example ... I tried to replicate in the
Northwinds database, by putting the new public function in a standard
module, but when I run the query Access tells me that there is an
'Undefined Function 'NullIfEqual' in the expression' ... what could I
be doing wrong? As far as I can tell everything is correct and nothing
has been misspelt??
I should add that I've noted the bug with MSA 2003 that's being
referenced here (http://support.microsoft.com/kb/824277) and I don't
think it's applicable, in this scenario I don't find any MISSING
references in the Visual Basic Editor ...
Feb 21 '08 #4
gi*************@googlemail.com wrote in news:c21ff808-4819-4572-b4bf-
f4**********@h11g2000prf.googlegroups.com:
On 21 Feb, 11:13, gillianbrook...@googlemail.com wrote:
>Lyle, thank you for this example ... I tried to replicate in the
Northwinds database, by putting the new public function in a standard
module, but when I run the query Access tells me that there is an
'Undefined Function 'NullIfEqual' in the expression' ... what could I
be doing wrong? As far as I can tell everything is correct and nothing
has been misspelt??

I should add that I've noted the bug with MSA 2003 that's being
referenced here (http://support.microsoft.com/kb/824277) and I don't
think it's applicable, in this scenario I don't find any MISSING
references in the Visual Basic Editor ...
You might want to check this a bit more with

Sub checkRefs()
Dim r As Reference
For Each r In References
Debug.Print r.Name, r.BuiltIn, r.IsBroken
Next r
End Sub
--
lyle fairfield

I will arise and go now,
For always night and day
I hear lake water lapping
With low sounds by the shore;
While I stand on the roadway
Or on the pavements gray,
I hear it in the deep heart's core.
- Yeats
Feb 21 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Phil Rutter | last post by:
Hello All, I have about 700 word documents that have 2 tables one is static 4 colums x 5 rows the other is 5 colums x rows ranging from 2 to 100 what i wolud like to do is open the word doc....
4
by: Maur | last post by:
Hi all, I have 2 tables say t_OLD and t_NEW. The new has corrections for audit purposes. They are identical in all respects (i.e. new is a copy of old and then changes are made to t_new) ...
2
by: windandwaves | last post by:
Hi Guys I want to find out if two tables are identical (e.g. same fields, same indexes, etc....). Not the data, but the table structure itself. Is there a smart way to do that? Cheers
0
by: dfs9 | last post by:
In the article "Delete Duplicate Records From Access Tables" By Danny Lesandrini writes the following: This final suggestion is the most flexible and accurate. Given any table, it generates a...
4
by: dfs9 | last post by:
In the article "Delete Duplicate Records From Access Tables" By Danny Lesandrini writes the following: This final suggestion is the most flexible and accurate. Given any table, it generates a...
21
by: Kristaps | last post by:
Hi everyone! I have some questions, maybe someone can help me... I write script for table comparing, but it works wery slovly. There is the script, can anyone give some tip how can I make this...
2
by: JonoB | last post by:
Need some tips to help me approach this problem. I would consider myself advanced in Access and VBA, so just looking for conceptual pointers. In essence, I have a number of text files, each of...
2
by: farouqdin | last post by:
Hi all i have code which loops through table and deletes the duplicate records. This code does it for one table. How do i change it so it goes through several tables? On Error Resume Next Dim...
3
by: DeanL | last post by:
Hi guys, Does anyone know of a way to create multiple tables using information stored in one table? I have a table with 4 columns (TableName, ColumnName, DataType, DataSize) and wanted to know...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.