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 to Sum in ASP pages

Status
Not open for further replies.

nonprogrammer

Technical User
Dec 28, 2005
143
US
Hi,

I would like to sum the values of a field and I am not sure how to go about it.

Code:
<%=(Recordset1.Fields.Item("dollar_amnt").Value)%>

I tried this but did not work

Code:
<%=sum(Recordset1.Fields.Item("dollar_amnt").Value)%>

<%sum(Recordset1.Fields.Item("dollar_amnt").Value)%>
 
Assuming that field dollar_amnt will always contain a numeric value you could do this:[tt]
TheSum = 0
Do While Not Recordset1.EoF
TheSum = TheSum + Recordset1("dollar_amnt")
Recordset1.MoveNext
Loop[/tt]


of course if the value could ever be Null or otherwise non-numeric you'd need a conditional test before attempting any arithmetic.
 
Sheco the field will always be numeric.

the way you describe it I should replace

Code:
<%=(Recordset1.Fields.Item("dollar_amnt").Value)%>



Code:
<%
TheSum = 0
Do While Not Recordset1.EoF
  TheSum = TheSum + Recordset1("dollar_amnt")
  Recordset1.MoveNext
Loop
%>
 
If you only care about the sum and not the individual values then perhaps you could change your SQL so that only the one summed value is returned.

SELECT Sum(dollar_amnt) FROM SomeTable
 
I have to have it by each user. If I do the sql then every one will be able to see everyone earnings
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top