Using a Do Until Loop to search for and modify data in a table

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

    Using a Do Until Loop to search for and modify data in a table

    Here is my code:
    Code:
        Dim db As DAO.Database
        Dim rs1 As DAO.Recordset
    
        Set db = CurrentDb()
        Set rs1 = db.OpenRecordset("CartUse")
        rs1.MoveFirst
      
        Do Until rs1.EOF
            If rs1.Fields(ID) = Val(ID) Then
                NumOrdTotal = Val(rs1.Fields(varLongMonth)) + Val(varNumOrd)
                txttest2 = Val(NumOrdTotal)
                rs1.Edit
                rs1.Fields(varLongMonth) = Val(NumOrdTotal)
                rs1.Update
            End If
            rs1.MoveNext
        Loop
    
      rs1.Close
      Set rs1 = Nothing
      Set db = Nothing
    What I am needing this to do is search through my table to match the value of the Field "ID" to the value of (ID), which is a txtBox on a form bound to another table. If it matches these two values then I need it to do the calculations in the If statement. It works for the first record, but will not work for any of the other records. Both ID fields are autonumber fields.
  • nico5038
    Recognized Expert Specialist
    • Nov 2006
    • 3080

    #2
    Not sure why you want this, as normally this is done by using a query.
    The problem with your code is the fact that you need to use:
    If rs1.Fields("ID" ) = Val(Me.ID) Then
    The " is needed to refer to the field value in the recordset, alternatively you could use:
    If rs1!ID = Val(Me.ID) Then
    When referring to form fields it's better to use the Me infront of the name.

    Nic;o)

    Comment

    • tehgreatmg
      New Member
      • Jan 2007
      • 49

      #3
      Thanks Nico that is all I needed.

      Comment

      • nico5038
        Recognized Expert Specialist
        • Nov 2006
        • 3080

        #4
        Glad I could help, success with the application !

        Nic;o)

        Comment

        Working...