-
Aug 16th, 2019, 09:18 AM
#1
Thread Starter
New Member
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
-
Aug 16th, 2019, 09:27 AM
#2
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
-
Aug 16th, 2019, 10:06 AM
#3
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.
-
Aug 16th, 2019, 11:18 AM
#4
Thread Starter
New Member
Re: Match records between two tables
Thanks techgnome
Can you advise with correct code that solve the issue
-
Aug 16th, 2019, 11:19 AM
#5
Thread Starter
New Member
Re: Match records between two tables
Thanks techgnome
Can you advise with correct code that solve the issue
-
Aug 16th, 2019, 11:20 AM
#6
Thread Starter
New Member
Re: Match records between two tables
Thanks wes4dbt
Can you help with correct code?
-
Aug 16th, 2019, 11:20 AM
#7
Thread Starter
New Member
Re: Match records between two tables
Thanks wes4dbt
Can you help with correct code?
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|