473,685 Members | 2,622 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Automatic Iterating through filtered subsets of a recordset

21 New Member
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 3719
FishVal
2,653 Recognized Expert Specialist
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 New Member
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 Recognized Expert Specialist
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 New Member
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.filte r as I tried in my first effort? Is that passed strictly as sql within the quotes?
Oct 8 '07 #5
FishVal
2,653 Recognized Expert Specialist
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.filte r 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 = "tractcount er = intcounter"
this
rst.Filter = "tractcount er = " & intcounter
Oct 9 '07 #6

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

Similar topics

6
9338
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 a checkbox with a control source named "MARK" (boolean) from customer table. I can check and uncheck individual records fine. Then I created 2 command buttons named "Select All" and "Deselect All". The Onclick property of these buttons runs code...
2
2360
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 values they filtered for. On the form, there could be 10 controls displaying that the user filters/sorts in whatever way they want to, but I only need to extract the control holding the PK values as a "recordset" so that I can use this to create a...
15
5004
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 detail area would be filtered. for example: i type a letter A in the textbox then the data would be filtered with the data started with an A, and if i continue typing for example : ABB then the data would be filtered with the data started with ABB and so...
4
1681
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. After each practice session, the utility program tells the flashcard program which items were learned and whihc need more practice. The drill stats are trracked by 3 indices: user, subject, and item.
3
3457
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: Access 2002 front-end using SQL Server 2000 (MSDE actually) via ADP/ADE Access Data Project.
16
3041
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. My Config: Access 2002 front-end using SQL Server 2000 (MSDE actually) via ADP/ADE Access Data Project. I have a form (containing about 80 fields) on which I allow the user to apply field/form filters -- this works fine.
18
1805
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. My code is working just fine, but I'm wondering if this is the most "elegant" way to do this. Essentially, I'm searching the dict self.contacts for a key that matches the pattern entered by the user. If so, I print the value associated with that...
2
19308
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 Excel. I don't want to use the docmd.outputTo due to it won't filter the records, it puts all of the records in the file. I've looked around and found some code (actually that I'm already using), but the problem is that it outputs EVERYTHING on the...
0
1464
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 to do is make an automatic filtering and labeling method for my table. The reason for this is because I m handling more than 500 records and labeling them would take me forever, so I started with what I can understand, which is seeking 5 records...
0
8577
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8512
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9048
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8763
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7591
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4303
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4525
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2199
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
1931
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.