whatever9987654
Technical User
Hey Guys,
I'm looking to speed up this code, it runs 3 queries to calculate data and append to each row. It runs slower as it progresses, the only thing I can think of is changing the 'For Next' to a 'For Each Next' statement. On a recorset of 2000 records it runs approx 2 records per second then slows down to 1 record a second, and so on. Anyway, I'm only a junior coder and would appreciate any help. Thanks guys!(and gals)
-MS
For Rcd = 30 To total_records Step 1
current_id = rs_id.Fields("id")
Set rs_value = db.OpenRecordset("SELECT NMBR as
current_number FROM data WHERE id
=" & current_id)
current_number = rs_value.Fields("current_number")
Set rs_sum = db.OpenRecordset("SELECT SUM(NMBR)
as current_sum FROM data WHERE id IN
(SELECT TOP 30 id from data WHERE id
<= " & current_id & " ORDER BY id
DESC)")
current_sum = rs_sum.Fields("current_sum")
current_average = current_sum / 30
current_difference = current_number -
current_average
db.Execute ("UPDATE data SET C_SUM=" & current_sum
& ", C_AVG=" & current_average & ",
C_DIF=" & current_difference & " WHERE
id=" & current_id)
rs_id.MovePrevious
Next Rcd
I'm looking to speed up this code, it runs 3 queries to calculate data and append to each row. It runs slower as it progresses, the only thing I can think of is changing the 'For Next' to a 'For Each Next' statement. On a recorset of 2000 records it runs approx 2 records per second then slows down to 1 record a second, and so on. Anyway, I'm only a junior coder and would appreciate any help. Thanks guys!(and gals)
-MS
For Rcd = 30 To total_records Step 1
current_id = rs_id.Fields("id")
Set rs_value = db.OpenRecordset("SELECT NMBR as
current_number FROM data WHERE id
=" & current_id)
current_number = rs_value.Fields("current_number")
Set rs_sum = db.OpenRecordset("SELECT SUM(NMBR)
as current_sum FROM data WHERE id IN
(SELECT TOP 30 id from data WHERE id
<= " & current_id & " ORDER BY id
DESC)")
current_sum = rs_sum.Fields("current_sum")
current_average = current_sum / 30
current_difference = current_number -
current_average
db.Execute ("UPDATE data SET C_SUM=" & current_sum
& ", C_AVG=" & current_average & ",
C_DIF=" & current_difference & " WHERE
id=" & current_id)
rs_id.MovePrevious
Next Rcd