Renumbering two fields in different tables at once

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tehgreatmg
    New Member
    • Jan 2007
    • 49

    Renumbering two fields in different tables at once

    Ok I have two tables both with an ID field, whenever a record is deleted these need to be renumbered to keep corresponding data. Here is the code I am using to do this:
    Code:
        Dim db As DAO.Database
        Dim rs2 As DAO.Recordset
        Dim rs3 As DAO.Recordset
    
        Set db = CurrentDb()
        Set rs2 = db.OpenRecordset("PrinterCartInventory")
        Set rs3 = db.OpenRecordset("CartUse")
        rs2.MoveFirst
        i = 1
        Do Until (rs2.EOF And rs3.EOF)
        
            rs2.Edit
            rs2!ID = i
            rs2.Update
            rs3.Edit
            rs3!ID = i
            rs3.Update
            i = i + 1
            rs2.MoveNext
            rs3.MoveNext
        Loop
    Whenever it runs it gives me an error saying no current record. I tried commenting out stuff to see where it was hanging up at and it is fine until rs3!ID=i. Does this mean this table needs focus to be able to do this or what?
  • nico5038
    Recognized Expert Specialist
    • Nov 2006
    • 3080

    #2
    It's not wise to change an ID field and seldomly needed.
    The only option would be to add an additional field and update that to reflect the sequence when you need a speedy solution, the more secure query solution would be:

    select ID, DCount("ID","tb lX","ID<=" & ID) as Sequence from tblX;

    This will give you the numbering needed, but once again, changing unique keys is often disastrous for the data's history. Why this need ?

    Nic;o)

    Comment

    • ADezii
      Recognized Expert Expert
      • Apr 2006
      • 8834

      #3
      Renumbering two fields in different tables at once

      Originally posted by tehgreatmg
      Ok I have two tables both with an ID field, whenever a record is deleted these need to be renumbered to keep corresponding data. Here is the code I am using to do this:
      Code:
          Dim db As DAO.Database
          Dim rs2 As DAO.Recordset
          Dim rs3 As DAO.Recordset
      
          Set db = CurrentDb()
          Set rs2 = db.OpenRecordset("PrinterCartInventory")
          Set rs3 = db.OpenRecordset("CartUse")
          rs2.MoveFirst
          i = 1
          Do Until (rs2.EOF And rs3.EOF)
          
              rs2.Edit
              rs2!ID = i
              rs2.Update
              rs3.Edit
              rs3!ID = i
              rs3.Update
              i = i + 1
              rs2.MoveNext
              rs3.MoveNext
          Loop
      Whenever it runs it gives me an error saying no current record. I tried commenting out stuff to see where it was hanging up at and it is fine until rs3!ID=i. Does this mean this table needs focus to be able to do this or what?
      I'm assuming that both Tables have exactly the same number of Records, because if they didn't, this code would never work. If either Recordset has more or less Records then the other, EOF would turn True for one Recordset and not the other. The MoveNext for the Recordset where EOF = True would now generate a Runtime Error. If in fact they do have the same number of Records, try an explicit MoveFirst on rs3:
      Code:
      rs2.MoveFirst
      [B]rs3.MoveFirst[/B]
      P.S. Nico gave you some excellant advice - you should take it.
      Last edited by ADezii; Feb 20 '07, 12:46 AM. Reason: Additional info

      Comment

      Working...