473,378 Members | 1,555 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Automatic Iterating through filtered subsets of a recordset

21
Hi there good folks,

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
  1. Sub addcounter()
  2.     Dim intcounter As Integer
  3.     Dim rst As ADODB.Recordset
  4.     Set rst = New ADODB.Recordset
  5.     Dim strsql As String
  6.     Dim trackcounter As Integer
  7.     strsql = "SELECT MAPBLKLOT, RESUNITS, parcelcounter, " & _
  8.              "tractcounter " & _
  9.              "FROM luse01; "
  10.  
  11.     rst.ActiveConnection = CurrentProject.Connection
  12.     rst.CursorType = adOpenDynamic
  13.     rst.LockType = adLockOptimistic
  14.  
  15.     'select all parcels into recordset
  16.     rst.Open strsql
  17.  
  18.     'iterate one time for each census tract and assign a number to all
  19.     'parcels in tract
  20.     For intcounter = 1 To 176
  21.  
  22.         'filter so incrementing counter is assigned to parcels in one tract
  23.         ' at a time
  24.         'the rst.filter below seems to be the problem. Can I not pass a 
  25.         'variable here within the filter string?
  26.         rst.Filter = "tractcounter = intcounter" 
  27.         Do Until rst.EOF
  28.             intcounter = intcounter + 1
  29.             rst!parcelcounter = intcounter
  30.             rst.Update
  31.         Loop
  32.     Next intcounter
  33.     Debug.Print intcounter & " records processed"
  34.     rst.Close
  35.     Set rst = Nothing
  36.  
  37. End Sub
Oct 8 '07 #1
5 3686
FishVal
2,653 Expert 2GB
Hi, askelo.

You are using the same variable as tractcounter and parcelcounter, but this has no influence as long as you don't generate filter expression properly (line #26). Suppose the results of code run are not the same you've been expecting.

BTW the same may be done using SQL with some VBA coding.

Expand|Select|Wrap|Line Numbers
  1. Public Function NumerateParcel(ByVal lngTractCounter As Integer) As Integer
  2.  
  3.     Static lngParcelCounter As Integer
  4.     Static lngCurrentTractCounter As Integer
  5.  
  6.     If lngCurrentTractCounter <> lngTractCounter Then
  7.         lngCurrentTractCounter = lngTractCounter
  8.         lngParcelCounter = 1
  9.     Else
  10.         lngParcelCounter = lngParcelCounter + 1
  11.     End If
  12.  
  13.     ParcelCounter = lngParcelCounter
  14.  
  15. End Function
  16.  
Expand|Select|Wrap|Line Numbers
  1. UPDATE (SELECT * FROM luse01 ORDER BY tractcounter) SET parcelcounter = NumerateParcel([tractcounter]);
  2.  
Oct 8 '07 #2
akselo
21
Hi, askelo.

You are using the same variable as tractcounter and parcelcounter, but this has no influence as long as you don't generate filter expression properly (line #26). Suppose the results of code run are not the same you've been expecting.

BTW the same may be done using SQL with some VBA coding.

Expand|Select|Wrap|Line Numbers
  1. Public Function NumerateParcel(ByVal lngTractCounter As Integer) As Integer
  2.  
  3.     Static lngParcelCounter As Integer
  4.     Static lngCurrentTractCounter As Integer
  5.  
  6.     If lngCurrentTractCounter <> lngTractCounter Then
  7.         lngCurrentTractCounter = lngTractCounter
  8.         lngParcelCounter = 1
  9.     Else
  10.         lngParcelCounter = lngParcelCounter + 1
  11.     End If
  12.  
  13.     ParcelCounter = lngParcelCounter
  14.  
  15. End Function
  16.  
Expand|Select|Wrap|Line Numbers
  1. UPDATE (SELECT * FROM luse01 ORDER BY tractcounter) SET parcelcounter = NumerateParcel([tractcounter]);
  2.  
How can you be so fast, and then on a holiday?? At any rate, the function does populate the field, but does so with 0 for all records/values of lngTractCounter ??
Oct 8 '07 #3
FishVal
2,653 Expert 2GB
How can you be so fast, and then on a holiday??
That isn't a problem if you know magic word. ;)

At any rate, the function does populate the field, but does so with 0 for all records/values of lngTractCounter ??
Aaaaa, just a so stupid mistake. Haven't changed function name when it returns value (last code line). Take a hot update. :)

Expand|Select|Wrap|Line Numbers
  1. Public Function NumerateParcel(ByVal lngTractCounter As Integer) As Integer
  2.  
  3.     Static lngParcelCounter As Integer
  4.     Static lngCurrentTractCounter As Integer
  5.  
  6.     If lngCurrentTractCounter <> lngTractCounter Then
  7.         lngCurrentTractCounter = lngTractCounter
  8.         lngParcelCounter = 1
  9.     Else
  10.         lngParcelCounter = lngParcelCounter + 1
  11.     End If
  12.  
  13.     NumerateParcel = lngParcelCounter
  14.  
  15. End Function
  16.  
Oct 8 '07 #4
akselo
21
That isn't a problem if you know magic word. ;)



Aaaaa, just a so stupid mistake. Haven't changed function name when it returns value (last code line). Take a hot update. :)

Expand|Select|Wrap|Line Numbers
  1. Public Function NumerateParcel(ByVal lngTractCounter As Integer) As Integer
  2.  
  3.     Static lngParcelCounter As Integer
  4.     Static lngCurrentTractCounter As Integer
  5.  
  6.     If lngCurrentTractCounter <> lngTractCounter Then
  7.         lngCurrentTractCounter = lngTractCounter
  8.         lngParcelCounter = 1
  9.     Else
  10.         lngParcelCounter = lngParcelCounter + 1
  11.     End If
  12.  
  13.     NumerateParcel = lngParcelCounter
  14.  
  15. End Function
  16.  
Well I should have seen that mistake had I paid attention. Now we know that I don't ;) With that fix, the run worked like a charm. THANKS!! For future reference, can you not pass an argument to recordset.filter as I tried in my first effort? Is that passed strictly as sql within the quotes?
Oct 8 '07 #5
FishVal
2,653 Expert 2GB
Well I should have seen that mistake had I paid attention. Now we know that I don't ;) With that fix, the run worked like a charm. THANKS!!
You are welcome. Hope you've got an idea rather than just copypasted code.

For future reference, can you not pass an argument to recordset.filter as I tried in my first effort? Is that passed strictly as sql within the quotes?
The value you pass to Recordset via Filter property has to be constant. Recordset object code has no idea whether intcounter is variable and what its value is.
So, instead
rst.Filter = "tractcounter = intcounter"
this
rst.Filter = "tractcounter = " & intcounter
Oct 9 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: GSteven | last post by:
(as formerly posted to microsoft.public.access.forms with no result) I've created a continuous form which is based on a straightforward table (ex - customers - 100 records). On the form there is...
2
by: KashMarsh | last post by:
Access 2003 I need to have a user filter records on a linked, continuous form and then I want to run various reports/queries from this recordset the user created. I only need to see the PK...
15
by: fbachri | last post by:
Hi, I have a form with 1 textbox in the header form area and data in continuous form style in detail area. i would like to make an automatic filter. so when i type a character the data in the...
4
by: LurfysMa | last post by:
I could use some help with a table design problem. I have an electronic flashcard program. Actually, several of them. They each rely on a utility program to keep track of the usage statistics....
3
by: melnhed | last post by:
---Report the current filtered records from a Form--- Hello All, I've seen this topic discussed before, but the solution described then doesn't work in my particular case. My Config: ...
16
by: radio1 | last post by:
---Report the current filtered records from a Form (in an ADP project)--- Hello All, I've seen this topic discussed before, but the solution described then doesn't work in my particular case. ...
18
by: Drew | last post by:
All - I'm currently writing a toy program as I learn python that acts as a simple address book. I've run across a situation in my search function where I want to iterate across a filtered list....
2
by: atlbearcat | last post by:
Here's one that's been bugging me for about a week now... I have a form that allows users to filter records, simple enough. But I want to give them the option to export the filtered records to...
0
by: cephal0n | last post by:
Hi All! I have two table tblHome1, contains all the unique PinNo and tblHome2 contains a duplicated PinNo and description. I put an automatic numbering (ProdID) and set it as my index. What I want...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.