fluffyozzy
Registered User.
- Local time
- Today, 22:35
- Joined
- May 29, 2004
- Messages
- 63
I'm sure this is really simple and I'm missing something silly. The following code works perfectly for what I want to do only if I specify the criteria in the sql statement.
Dim rst As DAO.Recordset
Dim rsta As DAO.Recordset
Dim data1, data2 As Integer
Dim db As Database
Dim sql As String
Dim sqla As String
sql = "SELECT * FROM tblBatch WHERE tblBatch.BatchNo = 17;"
sqla = "SELECT * from tblArchive WHERE tblArchive.BatchNo = 17;"
Set db = CurrentDb
Set rst = db.OpenRecordset(sql)
Set rsta = db.OpenRecordset(sqla)
data1 = rst("BatchNo")
data2 = rsta("BatchNo")
Do Until rst.EOF
If data1 = data2 Then
With rsta
.Edit
.Fields("BatchID") = rst!BatchID
.Fields("OrgID") = rst!OrgID
.Update
End With
End If
rsta.MoveNext
Loop
Both tables contain fields BatchID (PK in tblBatch and FK in tblArchive), BatchNo (this is a sequential number) and OrgID. The tblArchive has more than one corresponding items to each BatchID and at the moment, both the BatchID and OrgID are missing from the Archive table, however, records are identifiable by BatchNo (thankfully), so we know that we will be appending the right records.
What I want to do is to introduce another loop that moves through the tblBatch, find the corresponding items in the tblArchive, update the BatchID and OrgID records in tblArchive with the correct record, then move to the next record in tblBatch and loop. The above code works great if I specify the record to find but there are thousands of records so I'd like to automate the loop process. Could you please help?
I tried:
sql = "SELECT * FROM tblBatch;"
sqla = "SELECT * FROM tblArchive;"
Do Until rst.EOF
rst.MoveFirst
If data1 = data2 Then
Do Until rsta.EOF
With rsta
.Edit
.Fields("BatchID") = rst!BatchID
.Fields("OrgID") = rst!OrgID
.Update
End With
rsta.MoveNext
Loop
End If
rst.MoveNext
Loop
But this is just sending me into an endless loop! Played around quite a bit with it to figure out where loops should go and it's either not doing anything or looping constantly. I would be grateful for any help from you lovely gurus out there
Thank you in advance
Dim rst As DAO.Recordset
Dim rsta As DAO.Recordset
Dim data1, data2 As Integer
Dim db As Database
Dim sql As String
Dim sqla As String
sql = "SELECT * FROM tblBatch WHERE tblBatch.BatchNo = 17;"
sqla = "SELECT * from tblArchive WHERE tblArchive.BatchNo = 17;"
Set db = CurrentDb
Set rst = db.OpenRecordset(sql)
Set rsta = db.OpenRecordset(sqla)
data1 = rst("BatchNo")
data2 = rsta("BatchNo")
Do Until rst.EOF
If data1 = data2 Then
With rsta
.Edit
.Fields("BatchID") = rst!BatchID
.Fields("OrgID") = rst!OrgID
.Update
End With
End If
rsta.MoveNext
Loop
Both tables contain fields BatchID (PK in tblBatch and FK in tblArchive), BatchNo (this is a sequential number) and OrgID. The tblArchive has more than one corresponding items to each BatchID and at the moment, both the BatchID and OrgID are missing from the Archive table, however, records are identifiable by BatchNo (thankfully), so we know that we will be appending the right records.
What I want to do is to introduce another loop that moves through the tblBatch, find the corresponding items in the tblArchive, update the BatchID and OrgID records in tblArchive with the correct record, then move to the next record in tblBatch and loop. The above code works great if I specify the record to find but there are thousands of records so I'd like to automate the loop process. Could you please help?
I tried:
sql = "SELECT * FROM tblBatch;"
sqla = "SELECT * FROM tblArchive;"
Do Until rst.EOF
rst.MoveFirst
If data1 = data2 Then
Do Until rsta.EOF
With rsta
.Edit
.Fields("BatchID") = rst!BatchID
.Fields("OrgID") = rst!OrgID
.Update
End With
rsta.MoveNext
Loop
End If
rst.MoveNext
Loop
But this is just sending me into an endless loop! Played around quite a bit with it to figure out where loops should go and it's either not doing anything or looping constantly. I would be grateful for any help from you lovely gurus out there

Thank you in advance