I am trying to populate a column in a table with a number incrementing for each class (as set by another field; 176 different classes in table of 150,000 records representing parcels in a city), and then when a new class is present, restart the numbering until the next class and so on. The point is that I ultimately want to randomly select and apply changes to fields within each class, which is why I need the incrementing numbers as a field now).
In the code below, the sql statement calls parcelcounter (the field to be populated) and tractcounter, which for each record identifies which tract/class (from 1 to 176) the parcel belongs to. The crux is that I want to iterate through 176 iterations, with each changing the filter so it looks at a new subset of the recordset and numbers all parcels within that subset starting at 1 in each class.
Expand|Select|Wrap|Line Numbers
- Sub addcounter()
- Dim intcounter As Integer
- Dim rst As ADODB.Recordset
- Set rst = New ADODB.Recordset
- Dim strsql As String
- Dim trackcounter As Integer
- strsql = "SELECT MAPBLKLOT, RESUNITS, parcelcounter, " & _
- "tractcounter " & _
- "FROM luse01; "
- rst.ActiveConnection = CurrentProject.Connection
- rst.CursorType = adOpenDynamic
- rst.LockType = adLockOptimistic
- 'select all parcels into recordset
- rst.Open strsql
- 'iterate one time for each census tract and assign a number to all
- 'parcels in tract
- For intcounter = 1 To 176
- 'filter so incrementing counter is assigned to parcels in one tract
- ' at a time
- 'the rst.filter below seems to be the problem. Can I not pass a
- 'variable here within the filter string?
- rst.Filter = "tractcounter = intcounter"
- Do Until rst.EOF
- intcounter = intcounter + 1
- rst!parcelcounter = intcounter
- rst.Update
- Loop
- Next intcounter
- Debug.Print intcounter & " records processed"
- rst.Close
- Set rst = Nothing
- End Sub