Web Wiz - Green Windows Web Hosting

  New Posts New Posts RSS Feed - Adding values from SQL database
  FAQ FAQ  Forum Search   Events   Register Register  Login Login

Adding values from SQL database

 Post Reply Post Reply
Author
shanerobinson View Drop Down
Newbie
Newbie


Joined: 21 August 2008
Status: Offline
Points: 17
Post Options Post Options   Thanks (0) Thanks(0)   Quote shanerobinson Quote  Post ReplyReply Direct Link To This Post Topic: Adding values from SQL database
    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
Back to Top
Gullanian View Drop Down
Senior Member
Senior Member
Avatar

Joined: 04 January 2002
Location: England
Status: Offline
Points: 4373
Post Options Post Options   Thanks (0) Thanks(0)   Quote Gullanian Quote  Post ReplyReply Direct Link To This Post 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
Back to Top
shanerobinson View Drop Down
Newbie
Newbie


Joined: 21 August 2008
Status: Offline
Points: 17
Post Options Post Options   Thanks (0) Thanks(0)   Quote shanerobinson Quote  Post ReplyReply Direct Link To This Post Posted: 20 April 2011 at 12:46pm
Thanks for your help....
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down

Forum Software by Web Wiz Forums® version 12.08
Copyright ©2001-2026 Web Wiz Ltd.


Become a Fan on Facebook Follow us on X Connect with us on LinkedIn Web Wiz Blogs
About Web Wiz | Contact Web Wiz | Terms & Conditions | Cookies | Privacy Notice

Web Wiz is the trading name of Web Wiz Ltd. Company registration No. 05977755. Registered in England and Wales.
Registered office: Web Wiz Ltd, Unit 18, The Glenmore Centre, Fancy Road, Poole, Dorset, BH12 4FB, UK.

Prices exclude VAT at 20% unless otherwise stated. VAT No. GB988999105 - $, € prices shown as a guideline only.

Copyright ©2001-2026 Web Wiz Ltd. All rights reserved.