Print Page | Close Window

Adding values from SQL database

Printed From: Web Wiz Forums
Category: General Discussion
Forum Name: Classic ASP Discussion
Forum Description: Discussion on Active Server Pages (Classic ASP).
URL: https://forums.webwiz.net/forum_posts.asp?TID=29259
Printed Date: 29 March 2026 at 12:33pm
Software Version: Web Wiz Forums 12.08 - https://www.webwizforums.com


Topic: Adding values from SQL database
Posted By: shanerobinson
Subject: Adding values from SQL database
Date Posted: 18 April 2011 at 12:44pm
I'm currently trying to add (sum) the values from a field within an SQL database. I would like to do this without using SQL itself if possible.

I thought of using ASP/VB Script and so far have managed to display the top value within the field but i need to know how to do a loop or another method to add up all of the values within the recordset field.

This is my code so far;

Dim totalcost
Dim paymentdue

totalcost = (Recordset1.Fields.Item("pricetotal").Value)

paymentdue = totalcost



Replies:
Posted By: Gullanian
Date Posted: 19 April 2011 at 9:39am
Dim totalcost
Dim paymentdue

'Initialise
totalcost  = 0

strSQL = "SELECT priceTotal FROM tblYourTable"
rsCommon.open strSQL, adoCon
do until rsCommon.EOF
    totalcost = totalcost  + rsCommon("priceTotal")
rsCommon.movenext
loop
rsCommon.close

paymentdue = totalcost

You can speed the above up significantly by dumping the recordset into an array.

However, a far better way though is:

Dim totalcost

strSQL = "SELECT sum(priceTotal) as TotalCost FROM tblYourTable"
rsCommon.open strSQL, adoCon
    totalcost = rsCommon("priceTotal");
rsCommon.close


Posted By: shanerobinson
Date Posted: 20 April 2011 at 12:46pm
Thanks for your help....



Print Page | Close Window

Forum Software by Web Wiz Forums® version 12.08 - https://www.webwizforums.com
Copyright ©2001-2026 Web Wiz Ltd. - https://www.webwiz.net