Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I loop through records and get the sum of one field?

Status
Not open for further replies.

GICodeWarrior

Programmer
Jun 1, 2001
11
0
0
US
I wish to add up all of the Totals in one of my tables. I know literally nothing about VB.

Please help, Code Warrior Burchfield

I write code. See code run. Run code run.
 
You could use a running total variable. For example:

'Your variable will need to match the datatype of the field
'I'm assuming an integer variable here
Dim intTotal As Integer
...
'Each time you loop thru your recordset, add the value of
'the field you want to total to your running total variable
For i = 1 to objRS.Count
intTotal = intTotal + objRS.Fields("Total").Value
Next

Hope this helps you.

Rich
 
Not

"How can I loop through records and get the sum of one field?"

but

"Why can I loop through records and get the sum of one field?"

Just do an aggregate query. The "sigma" button on the query builder button bar. Also look at the help screen(s).


MichaelRed
redmsp@erols.com

There is never time to do it right but there is always time to do it over
 
You can do it directly in the query.
Here is an example that first sum all the fields and then add up all the totals:

SELECT
Sum(TestTable.TestField1) AS SumOfTestField1,
Sum(TestTable.TestField2) AS SumOfTestField2,
Sum(TestTable.TestField3) AS SumOfTestField3,
Sum([TestTable].[Testfield1]+[TestTable].[Testfield2]+[TestTable].[Testfield3]) AS TotalSum
FROM TestTable;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top