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
|