Question : Loop through table and number records

Is it possible / how would I loop through a table and sequentially number each record in field [CountMe]; starting over at 1 every time [RID] changes?

Answer : Loop through table and number records

Here is a simpler procedure; I think it will do what you want:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
Public Sub NumberRecords()
   
   Dim dbs As DAO.Database
   Dim rst As DAO.Recordset
   Dim strOldValue As String
   Dim strNewValue As String
   Dim lngCount As Long
   Dim strSQL As String
   
   Set dbs = CurrentDb
   Set rst = dbs.OpenRecordset("qryRID")
   
   'First clear old values
   strSQL = "UPDATE tblTest SET tblTest.CountMe = 0;"
   DoCmd.SetWarnings False
   DoCmd.RunSQL strSQL
   strOldValue = ""
   strNewValue = ""
   lngCount = 0
   
   'Update records, incrementing for each RID value
   Do While Not rst.EOF
      strNewValue = rst![RID]
      rst.Edit
      Debug.Print "Old value: " & strOldValue & "; New value: " & strNewValue
      If strNewValue <> strOldValue Then
         'RID has changed, restart incrementing
         lngCount = 0
      End If
      
      lngCount = lngCount + 1
      Debug.Print "Counter: " & lngCount
      rst![CountMe] = lngCount
      rst.Update
      strOldValue = strNewValue
      rst.MoveNext
   Loop
 
End Sub
Random Solutions  
 
programming4us programming4us