secondangel
Registered User.
- Local time
- Today, 18:49
- Joined
- Nov 6, 2008
- Messages
- 52
Hi there
I have table1 - rs1 containing records(33,000). I want to loop through each record. Then i have to find the matching record from table2 - rs2 (also about 33,000). So starting at rs1 - position 1 check and see if rs2 - position 1 matches. If it matches then copy a field from rs2 into rs1. set found as true.
If not movenext until you get to the end of rs2. if still no match at the end then back out and return a false match. (becuase there are about 100 which dont match.) either way if found or not then move to rs1 next and start back at beginning of rs2 again
My code below works but doesnt as it does not return the correct field value. I.e it just keeps the field it found when it first found a match. Can anyone help or think of a better design code.
thanks a lot
I have table1 - rs1 containing records(33,000). I want to loop through each record. Then i have to find the matching record from table2 - rs2 (also about 33,000). So starting at rs1 - position 1 check and see if rs2 - position 1 matches. If it matches then copy a field from rs2 into rs1. set found as true.
If not movenext until you get to the end of rs2. if still no match at the end then back out and return a false match. (becuase there are about 100 which dont match.) either way if found or not then move to rs1 next and start back at beginning of rs2 again
My code below works but doesnt as it does not return the correct field value. I.e it just keeps the field it found when it first found a match. Can anyone help or think of a better design code.
Code:
Dim db As DAO.Database
Dim rst1 As DAO.Recordset
Dim rst2 As DAO.Recordset
Set db = CurrentDb()
Set rst1 = db.OpenRecordset("WITHNEWCOST")
Set rst2 = db.OpenRecordset("WITHORIGINALCOST")
Dim checka As String
Dim checkb As String
Dim ccost As Double
Dim found As Integer
' OPEN original cost, take the first 3 letters and the qty as the check
' open the new cost, take the first 3 letters and the qty - check if it macthes above. if so - copy the original cost price over and write it into the new table.
' if not - write a value of 0.001 - to show its unknown - or if it allows leave as a blank NULL value
'RST1 IS THE ENWLY WRITTEN FILE
If rst2.BOF And rst2.EOF Then
MsgBox "table is empty", vbCritical
Else
rst1.MoveFirst
rst2.MoveFirst
' start at the top record
checkb = Left(rst2.Fields(0), 4) & "" & Str(rst2.Fields(2)) ' original cost table
Do Until rst1.EOF
found = 0
checka = Left(rst1.Fields(1), 4) & "" & Str(rst1.Fields(4)) ' new cost table
checkb = Left(rst2.Fields(0), 4) & "" & Str(rst2.Fields(2)) ' original cost table
Do Until found = 1 Or rst2.EOF
If checka = checkb Then ' do records match
found = 1
ccost = rst2.Fields("COST")
'else
' move to next rs2 record
Else
rst2.MoveFirst
Do Until rst2.EOF
checkb = Left(rst2.Fields(0), 4) & "" & Str(rst2.Fields(2)) ' original cost read the line in rs2
If checka = checkb Then ' so we have rs1 record and we are checking every rs2 record for a match
found = 1
ccost = rst2.Fields("COST")
Else
found = 0
ccost = 0
End If
rst2.MoveNext
Loop
End If
' either we have a match or eof.
Loop
rst1.Edit
rst1.Fields(6) = ccost
rst1.Update
checka = ""
checkb = ""
rst1.MoveNext
Loop
End If
MsgBox "all new records updated and matched", vbInformation