473,666 Members | 2,144 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

While loop to get start/end values of condition

6 New Member
Hello All,

I was hoping that someone might have a suggestion on the best way to complete the follwoing problem.

I have a bunch of roads. Each road has a Start and end Distance (chainage in metres) and may have been sealed in multiple sections over the last 40 years. Each seal section also has a start and an end value and they may overlap each other.

I have managed to get this information into a stocck type graph which displays the seal in a distance verses year type format. From this I can visually see the age of the road and how the sections should be broken up.

I want to be able to summarize this data to automatically get the the latest seal with a start and end value for the entrire length of road.

I see this kind of done in a while loop where X = total length of road,
XS = value of start of segments
XE = Vale of end of segment
Y = year of seal

therefore

XS = 0
XE= 0
while XE<= X

get Y value
increment XE value until Y value changes
write XS XE Y to table
set XS to equal last XE
and repeat until end of road reached.

The final result that I want is a table of ROAD_ID, Start, End, Year of seal.

Hopefully that all makes sense.

I am probably just searching for the wrong words but if anyone has any examples that I can refer to that would be fantastic.

Cheers,

Laura
Aug 10 '07 #1
16 2954
Rabbit
12,516 Recognized Expert Moderator MVP
It's a little hard to follow, can you post up an example of the data before hand, and what it should look like after?
Aug 10 '07 #2
ADezii
8,834 Recognized Expert Expert
Hello All,

I was hoping that someone might have a suggestion on the best way to complete the follwoing problem.

I have a bunch of roads. Each road has a Start and end Distance (chainage in metres) and may have been sealed in multiple sections over the last 40 years. Each seal section also has a start and an end value and they may overlap each other.

I have managed to get this information into a stocck type graph which displays the seal in a distance verses year type format. From this I can visually see the age of the road and how the sections should be broken up.

I want to be able to summarize this data to automatically get the the latest seal with a start and end value for the entrire length of road.

I see this kind of done in a while loop where X = total length of road,
XS = value of start of segments
XE = Vale of end of segment
Y = year of seal

therefore

XS = 0
XE= 0
while XE<= X

get Y value
increment XE value until Y value changes
write XS XE Y to table
set XS to equal last XE
and repeat until end of road reached.

The final result that I want is a table of ROAD_ID, Start, End, Year of seal.

Hopefully that all makes sense.

I am probably just searching for the wrong words but if anyone has any examples that I can refer to that would be fantastic.

Cheers,

Laura
Just subscribing and waiting for data from OP.
Aug 10 '07 #3
LODIE
6 New Member
Just subscribing and waiting for data from OP.
Sorry about that, I should have posted an example.

Just say that I have the following data for a road

Road_id start_Seal End_seal treatment year
100:01 0 100 primerseal 1981
100:01 0 100 finalseal 1992
100:01 50 200 reseal 1997
100:01 125 150 reseal 2001


I want to rebreak this information into logical segments which will give a linear timeline (in this case lengthline) to give distance vs age.

ie


Road_id start_Seal End_seal treatment year
100:01 0 50 final seal 1992
100:01 50 125 reseal 1997
100:01 125 150 reseal 2001
100:01 150 200 reseal 1997


segments are now 0-50, 50-125, 125-150, 150-200 with only the latest treatment and year associated with each distance along the road.

These results will be combined with other data into a formula that will be used to predict when a road will need to be rebuilt.

Hopefully that makes a little more sense....
Aug 12 '07 #4
ADezii
8,834 Recognized Expert Expert
Sorry about that, I should have posted an example.

Just say that I have the following data for a road

Road_id start_Seal End_seal treatment year
100:01 0 100 primerseal 1981
100:01 0 100 finalseal 1992
100:01 50 200 reseal 1997
100:01 125 150 reseal 2001


I want to rebreak this information into logical segments which will give a linear timeline (in this case lengthline) to give distance vs age.

ie


Road_id start_Seal End_seal treatment year
100:01 0 50 final seal 1992
100:01 50 125 reseal 1997
100:01 125 150 reseal 2001
100:01 150 200 reseal 1997


segments are now 0-50, 50-125, 125-150, 150-200 with only the latest treatment and year associated with each distance along the road.

These results will be combined with other data into a formula that will be used to predict when a road will need to be rebuilt.

Hopefully that makes a little more sense....
I'm gonna sleep on this one tonight and hopefully I'll have a solution for you soon. I'll be in touch.
Aug 13 '07 #5
Rabbit
12,516 Recognized Expert Moderator MVP
Complicated...

Gonna throw around an idea until I can get into the office and test it out. Forgive the rambling.

1) Take start, type, and year from current record.

2) Use start of next highest record as the end. Subquery.

3) If no higher record, use current end.

4) Keep record only if it's the most recent year for that start.

No... that's not gonna work. It won't give you the last record. Looks like we'll have to use VBA.
Aug 13 '07 #6
Rabbit
12,516 Recognized Expert Moderator MVP
Tested and verified.

DataTable and FinalTable
Road_id; Text
start_Seal; Number
End_seal; Number
treatment; Text
year; Number

Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Option Explicit
  3.  
  4. Public Sub RoadCondition()
  5.     Dim rst1 As Recordset
  6.     Dim rst2 As Recordset
  7.     Dim rst3 As Recordset
  8.     Dim rstart, rend, current As Integer
  9.     Dim cstart, cend As Integer
  10.  
  11.     ' Opens a record set of all unique Road_id.
  12.     Set rst1 = CurrentDb.OpenRecordset("SELECT DISTINCT Road_id FROM DataTable;")
  13.  
  14.     Do Until rst1.EOF
  15.  
  16.         ' Loops through all unique Road_id and sets start and end values of the Road_id
  17.         rstart = DMin("start_Seal", "DataTable", "Road_id = '" & rst1!Road_id & "'")
  18.         rend = DMax("End_seal", "DataTable", "Road_id = '" & rst1!Road_id & "'")
  19.  
  20.         ' Sets current position equal to start of road
  21.         current = rstart
  22.  
  23.         ' Loops until current position reaches end of road
  24.         Do Until current > rend
  25.  
  26.             ' Sets the values of the start of the section
  27.             cstart = current
  28.  
  29.             ' Gets the most current values for the start of the road section
  30.             Set rst2 = CurrentDb.OpenRecordset("SELECT TOP 1 * FROM DataTable WHERE Road_id = '" & rst1!Road_id & "'" & _
  31.                 " AND " & cstart & " BETWEEN start_Seal AND End_seal ORDER BY year DESC;")
  32.  
  33.             ' Retrieves the record with a start_Seal after the current position with a year later than current record's year
  34.             Set rst3 = CurrentDb.OpenRecordset("SELECT TOP 1 * FROM DataTable WHERE Road_id = '" & rst1!Road_id & "'" & _
  35.                 " AND start_Seal > " & cstart & " AND start_Seal < " & rst2!End_seal & " AND year > " & rst2!Year & " ORDER BY start_Seal, year DESC;")
  36.  
  37.             ' If no such record exists, use the current record's End_seal, if it does, use the next record's start_Seal
  38.             If rst3.RecordCount = 0 Then
  39.                 cend = rst2!End_seal
  40.             Else
  41.                 cend = rst3!start_Seal - 1
  42.             End If
  43.  
  44.             ' Inserts a record into final table for that section
  45.             DoCmd.SetWarnings False
  46.             DoCmd.RunSQL "INSERT INTO FinalTable (Road_id, start_Seal, End_seal, treatment, year) VALUES (" & _
  47.                 "'" & rst1!Road_id & "'," & cstart & "," & cend & ",'" & rst2!treatment & "'," & rst2!Year & ");"
  48.             DoCmd.SetWarnings True
  49.  
  50.             ' Increment to next road section
  51.             current = cend + 1
  52.         Loop
  53.  
  54.         ' Moves to next Road_id
  55.         rst1.MoveNext
  56.  
  57.     Loop
  58. End Sub
  59.  
  60. Private Sub Command0_Click()
  61.     ' Delete the records in the final table before calling the function
  62.     DoCmd.SetWarnings False
  63.     DoCmd.RunSQL "DELETE * FROM FinalTable"
  64.     DoCmd.SetWarnings True
  65.  
  66.     Call RoadCondition
  67. End Sub
  68.  
Aug 13 '07 #7
ADezii
8,834 Recognized Expert Expert
Tested and verified.

DataTable and FinalTable
Road_id; Text
start_Seal; Number
End_seal; Number
treatment; Text
year; Number

Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Option Explicit
  3.  
  4. Public Sub RoadCondition()
  5.     Dim rst1 As Recordset
  6.     Dim rst2 As Recordset
  7.     Dim rst3 As Recordset
  8.     Dim rstart, rend, current As Integer
  9.     Dim cstart, cend As Integer
  10.  
  11.     ' Opens a record set of all unique Road_id.
  12.     Set rst1 = CurrentDb.OpenRecordset("SELECT DISTINCT Road_id FROM DataTable;")
  13.  
  14.     Do Until rst1.EOF
  15.  
  16.         ' Loops through all unique Road_id and sets start and end values of the Road_id
  17.         rstart = DMin("start_Seal", "DataTable", "Road_id = '" & rst1!Road_id & "'")
  18.         rend = DMax("End_seal", "DataTable", "Road_id = '" & rst1!Road_id & "'")
  19.  
  20.         ' Sets current position equal to start of road
  21.         current = rstart
  22.  
  23.         ' Loops until current position reaches end of road
  24.         Do Until current > rend
  25.  
  26.             ' Sets the values of the start of the section
  27.             cstart = current
  28.  
  29.             ' Gets the most current values for the start of the road section
  30.             Set rst2 = CurrentDb.OpenRecordset("SELECT TOP 1 * FROM DataTable WHERE Road_id = '" & rst1!Road_id & "'" & _
  31.                 " AND " & cstart & " BETWEEN start_Seal AND End_seal ORDER BY year DESC;")
  32.  
  33.             ' Retrieves the record with a start_Seal after the current position with a year later than current record's year
  34.             Set rst3 = CurrentDb.OpenRecordset("SELECT TOP 1 * FROM DataTable WHERE Road_id = '" & rst1!Road_id & "'" & _
  35.                 " AND start_Seal > " & cstart & " AND start_Seal < " & rst2!End_seal & " AND year > " & rst2!Year & " ORDER BY start_Seal, year DESC;")
  36.  
  37.             ' If no such record exists, use the current record's End_seal, if it does, use the next record's start_Seal
  38.             If rst3.RecordCount = 0 Then
  39.                 cend = rst2!End_seal
  40.             Else
  41.                 cend = rst3!start_Seal - 1
  42.             End If
  43.  
  44.             ' Inserts a record into final table for that section
  45.             DoCmd.SetWarnings False
  46.             DoCmd.RunSQL "INSERT INTO FinalTable (Road_id, start_Seal, End_seal, treatment, year) VALUES (" & _
  47.                 "'" & rst1!Road_id & "'," & cstart & "," & cend & ",'" & rst2!treatment & "'," & rst2!Year & ");"
  48.             DoCmd.SetWarnings True
  49.  
  50.             ' Increment to next road section
  51.             current = cend + 1
  52.         Loop
  53.  
  54.         ' Moves to next Road_id
  55.         rst1.MoveNext
  56.  
  57.     Loop
  58. End Sub
  59.  
  60. Private Sub Command0_Click()
  61.     ' Delete the records in the final table before calling the function
  62.     DoCmd.SetWarnings False
  63.     DoCmd.RunSQL "DELETE * FROM FinalTable"
  64.     DoCmd.SetWarnings True
  65.  
  66.     Call RoadCondition
  67. End Sub
  68.  
Nice solution Rabbit, and thanks for saving me a lot of time especially since I was experiencing a mental block anyway, and probably would not have arrived at a solution. I am curious about one think - is there any special reason why rstart, cstart, and rend are declared as Variants?
Aug 13 '07 #8
ADezii
8,834 Recognized Expert Expert
Complicated...

Gonna throw around an idea until I can get into the office and test it out. Forgive the rambling.

1) Take start, type, and year from current record.

2) Use start of next highest record as the end. Subquery.

3) If no higher record, use current end.

4) Keep record only if it's the most recent year for that start.

No... that's not gonna work. It won't give you the last record. Looks like we'll have to use VBA.
Nice solution Rabbit, and thanks for saving me a lot of time especially since I was experiencing a mental block anyway, and probably would not have arrived at a solution. I am curious about one think - is there any special reason why rstart, cstart, and rend are declared as Variants?
Aug 13 '07 #9
Rabbit
12,516 Recognized Expert Moderator MVP
Nice solution Rabbit, and thanks for saving me a lot of time especially since I was experiencing a mental block anyway, and probably would not have arrived at a solution. I am curious about one think - is there any special reason why rstart, cstart, and rend are declared as Variants?
uhh.. the reason is that you should see an ophthalmologist ? Unless I made a mistake and you edited my post, I declared them as Integers. You were probably glancing through it and saw me declare the integers over two lines and thought the other was a different data type. I only did it over two lines on a whim.
Aug 13 '07 #10

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

Similar topics

33
3833
by: Diez B. Roggisch | last post by:
Hi, today I rummaged through the language spec to see whats in the for ... else: for me. I was sort of disappointed to learn that the else clauses simply gets executed after the loop-body - regardless of the loop beeing entered or not. So where is an actual use case for that feature? I imagined that the else-clause would only be executed if the loop body
24
2697
by: Andrew Koenig | last post by:
PEP 315 suggests that a statement such as do: x = foo() while x != 0: bar(x) be equivalent to while True:
4
3268
by: James E Koehler | last post by:
I can't get the WHILE statement to work in MySQL. The version of MySQL that I am using is: Ver 12.16 Distrib 4.0.6-gamma, for Win95/Win98 (i32) running on Windows MX. Here is the relevant section from the manual: 20.1.9.7 WHILE Statement
147
10084
by: Michael B Allen | last post by:
Should there be any preference between the following logically equivalent statements? while (1) { vs. for ( ;; ) { I suspect the answer is "no" but I'd like to know what the consensus is
6
71965
by: John Pass | last post by:
What is the difference between a While and Do While/Loop repetition structure. If they is no difference (as it seems) why do both exist?
12
1972
by: Howard | last post by:
Hello everyone (total VB.NET beginner here), I'm reading the "SAMS Teach Yourself VB.NET In 21 Days" book, and came across an exercise that I can't get to work. The exercise asks that you create a game that makes the user guess a number from 1-100, and you tell the user "lower" or "higher" as they input their guesses, until they guess the correct number, at which point you then tell the user "Correct". I tried using the While Loop, and...
16
3516
by: Claudio Grondi | last post by:
Sometimes it is known in advance, that the time spent in a loop will be in order of minutes or even hours, so it makes sense to optimize each element in the loop to make it run faster. One of instructions which can sure be optimized away is the check for the break condition, at least within the time where it is known that the loop will not reach it. Any idea how to write such a loop? e.g.
8
1823
by: voger | last post by:
Hi everyone Can anyone please explain this strange behavior? I have this function that is supposed to copy the contents of one array to one other array, but sorted. I don't know if the logic is correct but when I try to debug this the do/while loop results in an endless loop. The Two first values of the unsorted array are unsorted = 8
13
3775
by: icarus | last post by:
Hi all, i'm new to python. Learning on my own how to ask a user to finish a loop or not. For some reason, it behaves as infinite loop although I changed its condition. Please tell me what I'm doing wrong. Thanks in advance. condition = True while ( condition ):
0
8444
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
8869
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...
0
8781
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8551
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,...
1
6198
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5664
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2771
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2011
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1775
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.