Addition and Division Using ASP

Results 1 to 2 of 2

Thread: Addition and Division Using ASP

  1. #1
    Kevin Guest

    Default Addition and Division Using ASP

    Okay, I&#039m trying to do an election database and I want it to calculate the total number of votes AND I want it to figure the percentage of votes for each candidate. Is there a way to add records together? Ex: TotalVote = rsNews("Cand1") + rsNews("Cand2"), etc. and then Cand1pct = rsNews("Cand1")/TotalVote.<BR>It&#039s really starting to bug the hell out of me why I get a number that I shouldn&#039t be getting.

  2. #2
    Join Date
    Dec 1969

    Default RE: Addition and Division Using ASP

    &#062; It&#039s really starting to bug the hell out of me why I get a <BR>&#062; number that I shouldn&#039t be getting.<BR><BR>If you&#039d shown us your code, we probably could have fixed it in a jiffy. The "why" is certainly in the code somewhere.<BR><BR>But anyway...<BR><BR>You have to do the addition for *EACH* record in the recordset, separately!<BR><BR>That is, in your loop that processes the recordset. You don&#039t show us code, so I will have to sort of guess:<BR><BR>&#060;%<BR>...<BR>cand1Vote = 0<BR>cand2Vote = 0<BR>...<BR>Do Until rsNews.EOF<BR>&nbsp;&nbsp;&nbsp;&nbsp;&#039 note how we add onto totals already gotten so far! important! <BR>&nbsp;&nbsp;&nbsp;&nbsp;&#039 the CInt call may not be needed but can&#039t hurt<BR>&nbsp;&nbsp;&nbsp;&nbsp;cand1Vote = cand1Vote + CInt(rsNews("Cand1")) <BR>&nbsp;&nbsp;&nbsp;&nbsp;cand1Vote = cand2Vote + CInt(rsNews("Cand2")) <BR>&nbsp;&nbsp;&nbsp;&nbsp;...<BR>&nbsp;&nbsp;&nb sp;&nbsp;rsNews.MoveNext<BR>Loop<BR>...<BR>TotalVo te = cand1Vote + cand2Vote<BR>Cand1pct = cand1Vote / TotalVote<BR>Cand2pct = cand2Vote / TotalVote<BR><BR>%&#062;<BR><BR>****************** *******<BR><BR>NOW...<BR><BR>You *could* have done almost all of that in your SQL SELECT statement, instead. <BR><BR>SELECT Sum(Cand1) AS Cand1Vote, Sum(Cand2) AS Cand2Vote ...<BR><BR>And then you&#039d just do:<BR><BR>&#060;%<BR>TotalVote = RS("cand1Vote") + RS("cand2Vote")<BR>Cand1pct = RS("cand1Vote") / TotalVote<BR>Cand2pct = RS("cand2Vote") / TotalVote<BR>%&#062;<BR><BR>Which is better? Do you like bread or toast?<BR><BR>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts