Sum with recordset

herbertioz

Registered User.
Local time
Today, 22:42
Joined
Nov 6, 2009
Messages
66
Hi all!

I have a recordset and need to calculate a running sum in the table "scale1". It works with one weight put into the table one for one, but not when you put in multiple records with weights.
I know I can use DSum(). Some suggestions on runningsum with recordset?

Code:
Dim d As Database
Dim v1 As Recordset
Dim Weight1 As Field
Dim Sum1 As Double

Set d = CurrentDb()
   Set v1 = d.OpenRecordset("scale1")
   Set Weight1 = v1.Fields("Weight")
   While Not v1.EOF

      Sum1 = (Sum1 + Weight1) / 1000
      v1.Delete
      v1.MoveNext

      Wend
    
   v1.Close
 
You forgot to explain what "doesn't work" means. Banish that expression from your vocabulary, because it is meaningless to anyone not seeing your screen or reading your mind :D Always say what you did, what the system did, and what you expected/desired.
 
I have not said "doesn't work", but I can explain the issue further yes:)

My goal is to make a running sum from the field "Weight" in the table "scale1".
Start at the top of the record, for example go down record for record and sum the weights together:

id weight (kg)
1 1000
2 1000
3 1000

1000+1000+1000=3000

Put the result in a variable like "Sum1".
 
Not to nitpick, but

It works with one weight put into the table one for one, but not

in any case, what does your code do that you do not like? It is a bit silly to keep asking you the same question, so this is my last attempt. Also, do not divide the sum by 1000 in each round, only afterwards.
 
Hehe:) I solve the problem my self. Thanks.

The solution was

Code:
Sum1 = Sum1 + v1("Weight")

instead of

Code:
Sum1 = (Sum1 + Vekt1)
 

Users who are viewing this thread

Back
Top Bottom