Match records between two tables
I have the below code to do matching between two different tables. The code only updates the first record as "Matched".
I want to compare each record in ID field from T1 if it present in T2, e.g. To check if A present in T2 then go to next record in T1 and check B if it present in T2 through loop until all records in T1 matched
Table 1
ID
A
B
C
Table 2
ID
A
B
Expected Matching Results
ID
A
B
Any help please
If rs2("ID").Value = rs1("ID").Value Then
rs2.MoveNext()
Do While Not rs2.EOF()
rs1("Matching").Value = "Matched"
rs1.Update()
rs2.MoveFirst()
Loop
End If
Re: Match records between two tables
This is much easier to do in SQL:
Code:
Update table1
Set field = newValue
from table1
inner join table2 on table1.id = table2.id
But to directly answer the question, it only updates the first record of Table1 because you never move to the second record. You only move through table2.
You need to loop through both tables.
-tg
Re: Match records between two tables
Plus you have an endless loop,
Code:
Do While Not rs2.EOF()
rs1("Matching").Value = "Matched"
rs1.Update()
rs2.MoveFirst()
Loop
This "rs2.MoveFirst()" need to be moved outside of the loop.
Re: Match records between two tables
Thanks techgnome
Can you advise with correct code that solve the issue
Re: Match records between two tables
Thanks techgnome
Can you advise with correct code that solve the issue
Re: Match records between two tables
Thanks wes4dbt
Can you help with correct code?
Re: Match records between two tables
Thanks wes4dbt
Can you help with correct code?