Looping 2 recordsets and adding/ updating

mcdhappy80

Registered User.
Local time
Today, 14:22
Joined
Jun 22, 2009
Messages
347
I have one table (tblAccount) in which I hold data, and second one (tblAssign) in which I want to copy specific records from the first table
. Records in tblAccount have three important columns. First is Autonumber PK column [intAccountNoID], second is [dteAccountDate] which holds the date account was opened, and third is [txtPersonID] which holds the ID fthe person which opened account.
The second table has several columns, but one that is of importance to us is [txtPersonID] which is a part of composite Primary key, along with to other date columns.
The scenario is following: A person can open multiple account which all have unique ID [intAccountNoID] but are different in date [dteAccountDate].
I need to read all the data entered in tblAccount (and I'm doing that via recordset loop) but copy only those account informations in tblAssign, which are newer in date [dteAccountDate].
The method I'm trying to implement uses two RS loops (rs1 and rs4), one inside another.
It works well in the first passthrough on the second record set (rs4), and it takes the newest records for the same pk [txtPersonID] based on date [dteAccountDate], but when I the code wants to add second records I'm getting the error message.
This is justified and I understant why it happens (because I'm adding as new a second [txtPersonID] which is already added on the first pass and that column is PK in the new table and I can't have two sam [txtPesronID]s which is what I want.
The error occurs on
Code:
rs2.Update
line.
I tried various thing to overcome this problem but with no luck.
How do I check in this part of code:
Code:
rs2.AddNew
rs2![txtSifraDetetaID] = rs1![txtSifraDetetaID]
rs2.Update
whether txtSifraDetetaID which I added on the first run, matches with the one on the current record and cancel update?

Here's the code that does the loop:
Code:
rs1.MoveFirst
        Do Until rs1.EOF
            
            rs2.AddNew
            rs2![txtSifraDetetaID] = rs1![txtSifraDetetaID]
            rs2.Update
           
            txtSifraDetetaID1 = rs1![txtSifraDetetaID]
            dteDatumZahteva1 = rs1![dteDatumZahteva]
            
            rs4.MoveFirst
            Do Until rs4.EOF

            txtSifraDetetaID2 = rs4![txtSifraDetetaID]
            dteDatumZahteva2 = rs4![dteDatumZahteva]
            
            If txtSifraDetetaID1 = txtSifraDetetaID2 Then
                If Not (dteDatumZahteva1 = dteDatumZahteva2) Then
                    If dteDatumZahteva1 < dteDatumZahteva2 Then
                        rs2.MoveFirst
                        
                        rs2.Edit
                        rs2![txtSifraDetetaID] = rs4![txtSifraDetetaID]
          
                        rs2.Update
                    Else
                        rs2.MoveFirst
                        
                        rs2.Edit
                        rs2![txtSifraDetetaID] = rs1![txtSifraDetetaID]
                        
                        rs2.Update
                    End If
                End If
                
            End If
            
            rs4.MoveNext
            Loop

        rs1.MoveNext
        Loop
 

Users who are viewing this thread

Back
Top Bottom