I need help in speeding up a process of looping through a recordset and grabbing a value while I'm looping.
There is about 40,000 records so it takes a long time to do.
First let me display an example table I'm using:
level levelAbove qty
------ ----------- -----
levelA none 1
levelB levelA 1
levelC levelB 2
levelD levelB 3
The "levelAbove" field is a field that stores the level above a specific level. So, levels levelC and levelD belong to levelB.
So let's use levelD. First I want to grab the qty of levelD. Next I look at levelD's levelAbove which is levelB and move to levelB's record then I grab that quantity which is 1. Then I search for the levelB's levelAbove which is levelA and move to that record and grab levelA' quantity which is 1. So it's like a continuous loop until I get to the top level.
Then I multiply the quantities 3 * 1 * 1.
Then I put this value in a table so I could do a sum for all the values.
My code looks something like this
sql = "select * from table"
set rst = currentdb.openrecordset(sql)
strlevel = rst("level"
product = 1
Do until rst.eof
Do until strlevel = "none"
rst.FindFirst "level='" & strlevel & "'"
product = rst!qty * product
strLevel = rst!levelAbove
Loop
With rst2
.AddNew
!level = rst("level"
!qty = product
.Update
End With
rst.MoveNext
Loop
Can I do this different so it speeds up the process?
Thanks for your help
Ramon
There is about 40,000 records so it takes a long time to do.
First let me display an example table I'm using:
level levelAbove qty
------ ----------- -----
levelA none 1
levelB levelA 1
levelC levelB 2
levelD levelB 3
The "levelAbove" field is a field that stores the level above a specific level. So, levels levelC and levelD belong to levelB.
So let's use levelD. First I want to grab the qty of levelD. Next I look at levelD's levelAbove which is levelB and move to levelB's record then I grab that quantity which is 1. Then I search for the levelB's levelAbove which is levelA and move to that record and grab levelA' quantity which is 1. So it's like a continuous loop until I get to the top level.
Then I multiply the quantities 3 * 1 * 1.
Then I put this value in a table so I could do a sum for all the values.
My code looks something like this
sql = "select * from table"
set rst = currentdb.openrecordset(sql)
strlevel = rst("level"
product = 1
Do until rst.eof
Do until strlevel = "none"
rst.FindFirst "level='" & strlevel & "'"
product = rst!qty * product
strLevel = rst!levelAbove
Loop
With rst2
.AddNew
!level = rst("level"
!qty = product
.Update
End With
rst.MoveNext
Loop
Can I do this different so it speeds up the process?
Thanks for your help
Ramon