473,378 Members | 1,493 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.

query to increment record numbers

Hi,

I bet the question below has been answered many times before, but I
have not found an answer. If someone could point me to one that would
be great!

I have the following table:

num run
1 1
2 0
3 0
4 1
5 0
6 1
7 0
8 0
9 0
10 0
....etc...

The records are ordered according to [num], and the [run] field has a
"1" that marks the beginning of a new run and zeros elsewhere.

I want to transform the table to look like this:

num run
1 1
2 2
3 3
4 1
5 2
6 1
7 2
8 3
9 4
10 5
....etc...

Here the zero records in the [run] field are replaced by the value in
the previous record +1. Each time a [run] record in the original table
has a one, the new table's [run] record value begins again with a one.

I am a novice Access user, and I thought I would write a loop something
like the following to make the transformation

while Dmin([run]=0) do the following "update query"

Iif(
DLookUp("[run]","[table]","[num]=" & [num])>0,
DLookUp("[run]","[table]","[num]=" & [num]),
IIf(
DLookUp("[run]","[table]","[num]=" & [num]-1)=0,
0,
DLookUp("[run]","[table]","[num]=" & [num]-1)+1
)
)
end while

The update query which uses the nested Iif statement inside the while
loop, seems to work fine. But it is very slow (on a file with 20,000
records), and my files have run lengths in the 100s, so the loop would
run 100s of times.

Is there are easier/faster/simpler way to do all of this?

Thanks in advance for any help!
Noah

Jun 17 '06 #1
4 3485
Noah wrote:
Hi,

I bet the question below has been answered many times before, but I
have not found an answer. If someone could point me to one that would
be great!

I have the following table:

num run
1 1
2 0
3 0
4 1
5 0
6 1
7 0
8 0
9 0
10 0
...etc...

The records are ordered according to [num], and the [run] field has a
"1" that marks the beginning of a new run and zeros elsewhere.

I want to transform the table to look like this:

num run
1 1
2 2
3 3
4 1
5 2
6 1
7 2
8 3
9 4
10 5
...etc...

Here the zero records in the [run] field are replaced by the value in
the previous record +1. Each time a [run] record in the original table
has a one, the new table's [run] record value begins again with a one.

I am a novice Access user, and I thought I would write a loop something
like the following to make the transformation

while Dmin([run]=0) do the following "update query"

Iif(
DLookUp("[run]","[table]","[num]=" & [num])>0,
DLookUp("[run]","[table]","[num]=" & [num]),
IIf(
DLookUp("[run]","[table]","[num]=" & [num]-1)=0,
0,
DLookUp("[run]","[table]","[num]=" & [num]-1)+1
)
)
end while

The update query which uses the nested Iif statement inside the while
loop, seems to work fine. But it is very slow (on a file with 20,000
records), and my files have run lengths in the 100s, so the loop would
run 100s of times.

Is there are easier/faster/simpler way to do all of this?

Thanks in advance for any help!
Noah

You could write a routine. This is aircode. I'll assume "RUN" is indexed

Sub UpdateRunNum
Dim lngCounter As Long
Dim strSQL As String
Dim rst As DAO.Recordset

strSQL = ""Select Num, Run From Table Order By Num"
set rst = currentdb.openrecordset(strSQL,dbopendynaset)

If rst.RecordCount > 0 then
'see if there are any records to update.
Do while True
'find the first "0" run
rst.FindFirst "Run = 0"
If not rst.NoMatch Then
'one was found.
'go to previous record and get the counter
'the counter might have been previously update
'and then more zero records addedd so...
rst.MovePrevious
lngCounter = rst!Run
rst.MoveNext
Do While True
If rst!Run = 0 then
lngCounter = lngCounter + 1
rst.Edit
Rst!Run = lngCounter
rst!Update
rst.MoveNext
If rst.EOF Then Exit Do
Else
exit DO
Endif
Loop
Exit DO
Else
Exit DO
Endif
Loop
Endif
rst.close
set rst = Nothing
Msgbox "Done"
End Sub
Jun 17 '06 #2
I'll give it a whirl. Thanks!

Jun 17 '06 #3
Noah wrote:
I'll give it a whirl. Thanks!

If you "give it a whirl" please make a backup of the table first. Run
it on the copy (all you need to do is change the table name in the SQL
statement) first and then review the results.
Jun 18 '06 #4
Noah, have you thought of reorganizing your table to look like this:

num run
1 1
2 1
3 1
4 2
5 2
6 3
7 3
8 3
9 3
10 3

that way, you could keep track of the order within a run by just using the
order of the 'num' field itself, and you could easily query to get just the
records that belong to a particular run.

Also, if you are just wanting to transform your table one time to get a
bunch of data in the shape form you want it, you may find that dumping it
into excel, processing it, and then moving it back into Access may be very
helpful.
-John

"Noah" <ga**@wharton.upenn.edu> wrote in message
news:11*********************@h76g2000cwa.googlegro ups.com...
Hi,

I bet the question below has been answered many times before, but I
have not found an answer. If someone could point me to one that would
be great!

I have the following table:

num run
1 1
2 0
3 0
4 1
5 0
6 1
7 0
8 0
9 0
10 0
...etc...

The records are ordered according to [num], and the [run] field has a
"1" that marks the beginning of a new run and zeros elsewhere.

I want to transform the table to look like this:

num run
1 1
2 2
3 3
4 1
5 2
6 1
7 2
8 3
9 4
10 5
...etc...

Here the zero records in the [run] field are replaced by the value in
the previous record +1. Each time a [run] record in the original table
has a one, the new table's [run] record value begins again with a one.

I am a novice Access user, and I thought I would write a loop something
like the following to make the transformation

while Dmin([run]=0) do the following "update query"

Iif(
DLookUp("[run]","[table]","[num]=" & [num])>0,
DLookUp("[run]","[table]","[num]=" & [num]),
IIf(
DLookUp("[run]","[table]","[num]=" & [num]-1)=0,
0,
DLookUp("[run]","[table]","[num]=" & [num]-1)+1
)
)
end while

The update query which uses the nested Iif statement inside the while
loop, seems to work fine. But it is very slow (on a file with 20,000
records), and my files have run lengths in the 100s, so the loop would
run 100s of times.

Is there are easier/faster/simpler way to do all of this?

Thanks in advance for any help!
Noah

Jun 18 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: curwen | last post by:
Hi, I'm in deep struggle with query from a huge table: what I've got is a lot of records like: _______________ numb fk1 _______________ 1231456 61 1231456 62
2
by: Maxi | last post by:
I have 101 fields (Field name P1, P2 .... P100 and Result) P1 to P100 has random numbers from 1 to 10. I want to run a query to update the 101st field (Result). It should check the entire record...
13
by: Maxi | last post by:
I have a table (Table name : Lotto) with 23 fields (D_No, DrawDate, P1, P2,.....P21) and it has draw results from 1st Sep 2004 till date. I have another table (Table name : Check) with 15 fields...
8
by: Maxi | last post by:
There is a lotto system which picks 21 numbers every day out of 80 numbers. I have a table (name:Lotto) with 22 fields (name:Date,P1,P2....P21) Here is the structure and sample data: ...
6
by: Dixie | last post by:
I have asked this question before, but I could not get the suggested solution work. So I will give more details this time. I have an append query that adds several hundred records to a table...
2
by: john | last post by:
Is it true that if I split my access database in backend and frontend and I implement custom auto increment for the ID fields, that my database is ready to be used in a multi-user environment? I...
6
by: Phil Stanton | last post by:
I am running a query that calls a function used to format addresses depending on the width of a control on a report that shows that address. The same query is used as the RecordSource of lots of...
5
by: David Wright | last post by:
Hello Everyone I would be grateful if someone could help me with the automatic increment of a field on my subform called ‘Test_SrNo’. I am Using Microsoft Office 2000. The auto entry of the...
1
AaronL
by: AaronL | last post by:
Hello, First I would like to say thank you all for your help in the past. I am stumped again. I am creating an e-commerce system and I want to be able to upload images to the server and...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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:
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
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.