Nick G
Nick G

Reputation: 1229

ASP Do While loop totals and subtotals

I don't claim to be a classic ASP guy, but sometimes you gotta do what you gotta do. My problem is that this is a custom built report through ASP and the user wants running SubTotals and a Grand Total displayed as well, and honestly I'm just not sure how to go about it. Currently the entire report is wrapped like this, the way it's displayed doesn't matter too much to the issue.

<%do while (adoRsTrade.AbsolutePage = iPageCurrent) and (not adoRsTrade.EOF)
NewRep = adoRsTrade("calIncludedRep")

if CurRep <> NewRep or FirstTime="T" and (not adorsTrade.BOF) then %>

add up the values for that CurRep and display them as 'subtotals'

<%

FirstTime="F"
CurRep = adoRsTrade("calIncludedRep")
adoRsTrade.MoveNext
loop %>

any help i could get would be greatly appreciated as i mentioned I am not an ASP guru by any means. Thank you, NickG

to update the subtotal stuff; the grand totals are completed since i can just re-run a do while loop outside of the loop the displays all of the data and total it, easy-peezy (not really) now what i run into is that the .MoveNext is hit prior to me being able to check the NewRep; here is the totalling of sub-totals as well as the check if it's a newrep which means display the sub-totals for the former rep

<%do while (adoRsTrade.AbsolutePage = iPageCurrent) and (not adoRsTrade.EOF)
NewRep = adoRsTrade("calIncludedRep")

if CurRep = NewRep then
        curPrincipal = adoRsTrade("mPrincipal")
        totPrincipal = totPrincipal + curPrincipal
            curInterest = adoRsTrade("mInterest")
            totInterest = totInterest + curInterest
        curCommission = adoRsTrade("calCommission")
        totCommission = totCommission + curCommission
            curSECFee = adoRsTrade("mSECFee")
            totSECFee = totSECFee + curSECFee
        curSvcFee = adoRsTrade("mSvcCharge")
        totSvcFee = totSvcFee + curSvcFee
            curNet = adoRsTrade("mNetAmount")
            totNet = totNet + curNet
end if    
if CurRep <> NewRep or FirstTime="T" and (not adorsTrade.BOF) then
  If FirstTime <> "T" then%>
      <TR>
        <td>
            <table class='FontStandardMinus1' border=0 cellPadding=0 align='left' cellSpacing=0 width="100%" bgcolor='#ffffff'>
                 <TR>
                    <td width="59%" align="left"><b>SubTotals<!-- for <%Response.Write(CurRep) %>-->:</b></td>
                    <td width="10%" valign=top align=right><%=FormatNumber(totPrincipal,2)%></td>
                    <td width="7%" valign=top align=right><%=FormatNumber(totInterest,2)%></td>
                    <td width="7%" valign=top align=right><%=FormatNumber(totCommission,2)%></td>
                    <td width="5%" valign=top align=right><%=FormatNumber(totSECFee,2)%></td>
                    <td width="4%" valign=top align=right><%=FormatNumber(totSvcFee,2)%></td>
                    <td width="9%" valign=top align=right><%=FormatNumber(totNet,2)%></td>
                </TR>
            </table>
        </td>
    </TR>
<%end if      
curPrincipal = 0
totPrincipal = 0
curInterest = 0
totInterest = 0
curCommission = 0
totCommission = 0
curSECFee = 0
totSECFee = 0
curSvcFee = 0
totSvcFee = 0
curNet = 0
totNet = 0 %>
<TR>
<TD width="100%">
<table class='FontStandardMinus1' border=0 cellPadding=0 align='left' cellSpacing=0 width="100%" bgcolor='#ffffff'>
<tr>
<td width=100%><b><%=adoRsTrade("calIncludedRep")%></b></td>

</tr>
</table>
</TD>
</TR>
<%end if%>
<%

FirstTime="F"
'CalCom = CalCom + adoRsTrade("calCommission")
CurRep = adoRsTrade("calIncludedRep")
adoRsTrade.MoveNext

loop%>

so you can see i try to reset the values to 0 after displaying them in a table, but that table is only displayed after the .MoveNext is hit in the hit, which means even though i want to display records 1-5 added up, record 6 was already hit by the .MoveNext and is being discarded from the value set. Sorry for the lengthiness of this but I hate that comments can't contain updated code. thanks for any help

Upvotes: 0

Views: 3906

Answers (1)

John
John

Reputation: 1327

Assuming that you output the variables totPrincipal, totInterest , totCommission after the loop the only problem i can think of is the datatype and the rs.MoveFirst.

BTW: Try do do it in a single loop for performance reasons!

Dim subtotPrincipal: subtotPrincipal = 0
Dim totPrincipal: totPrincipal = 0
Dim CurRep: CurRep = 0
Dim NewRep: NewRep = 0

adoRsTrade.MoveFirst
Do 
    NewRep = adoRsTrade("calIncludedRep")       
    ...
    subtotPrincipal = subtotPrincipal + CLng(adoRsTrade("mPrincipal"))
    totPrincipal = totPrincipal + CLng(adoRsTrade("mPrincipal"))
    ...

    If (CurRep <> NewRep And Not adorsTrade.BOF) Or adoRsTrade.EOF Then
        Response.Write subtotPrincipal
        subtotPrincipal = 0
    End If

    CurRep = NewRep
    adorsTrade.MoveNext
Loop Until adoRsTrade.EOF

Response.Write totPrincipal

Upvotes: 1

Related Questions