#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2008
    Posts
    28
    Rep Power
    0

    How to add up total cost


    Hello,

    I like to create a total cost column at the end. This field only adds up the total of 2 fields (aFees + fFees). For example, when
    I enter $100 in aFees box, the total box should show $100. If I enter $50 in fFees box, then the total should show $150.

    However, it didn't work.

    Here is a block of code. Can anyone help please?

    Thanks.

    Response.Write " <tr>"
    Response.Write " <td>Airplane Fees:</td>"
    Response.Write " <td><input type=""text"" name=""aFees"" size=24 value=""$" & rsData("aFees") & """></td>"
    Response.Write " <tr>"
    Response.Write " <td width=150>Food Fees: </td>"
    Response.Write " <td><input type=""text"" name=""fFees"" size=24 value=""$" & rsData("fFees") & """></td>"
    Response.Write " </tr>"
    Response.Write " <tr>"
    Response.Write " <td>TOTAL: </td>"

    Dim totalCost, aFees, fFees, cost
    While Not rsData.EOF
    totalCost = trim(rsData.Fields("aFees"))+trim(rsData.Fields("fFees"))
    cost = cost + totalCost
    Wend

    Response.Write " <td><input type=""text"" name=""cost"" size=24 value=""$" & rsData("cost") & """></td>"
    Response.Write " </tr>"
  2. #2
  3. No Profile Picture
    Grumpier old Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jun 2003
    Posts
    14,445
    Rep Power
    4539
    "Didn't work" means what? Do you get errors?
    ======
    Doug G
    ======
    Bartender to Rene Descartes "have another beer?" Descartes: "I think not" and he vanished.
    --Alfred Bester
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    20
    Rep Power
    0
    All the "Response.Write" statements are executed at the server,
    to format the page being displayed on your computer.

    At that point, the server is no longer involved with you or your page.

    If you type something into anything on the screen on your PC, the server has no knowledge or interest in this... UNLESS you re-invoke some logic on the server.

    It sounds to me like you should be using Javascript (which works on your PC) to perform the addition and update the info on your screen.

    Re-invoking your asp (server) module to do so is possible, but this seems onerous and is probably a waste of resources.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2008
    Posts
    28
    Rep Power
    0
    Hi, it didn't calculate at all. The tCost showed the last row.
    Thanks.

    Here is my modified code with tCost:

    <table width="960" align="center">
    <form method="Department">
    <tr height="25">
    <td class="a">&nbsp;Department Name</td>
    <td class="a">&nbsp;Student</td>
    <td class="a">&nbsp;Start</td>
    <td class="a">&nbsp;End</td>
    <td class="a">&nbsp;Cost</td>
    </tr>
    <% tCost =0
    While Not oRs.EOF %>
    <tr>
    <td><%=oRs("Department")%>&nbsp;</td>
    <td><%=oRs("Name")%>&nbsp;</td>
    <td><%=FormatDateTime(Month(oRs("StartDate")) & "/" & Day(oRs("StartDate")) & "/" & Year(oRs("StartDate")))%>&nbsp;</td>
    <td><%=FormatDateTime(Month(oRs("EndDate")) & "/" & Day(oRs("EndDate")) & "/" & Year(oRs("EndDate")))%>&nbsp;</td>
    <td <%=sRowStyle%>>$<%=oRs("Cost")%>&nbsp;</td>
    <% tCost = tCost + oRs("Cost") %>
    </tr>
    <% oRs.MoveNext %>
    <% WEND %>
    <tr>
    <td class="a">&nbsp;</td>
    <td class="a">&nbsp;</td>
    <td class="a">&nbsp;/td>
    <td class="a">&nbsp;Total</td>
    <td class="a">&nbsp;<%=tCost%></td>
    </tr>
    </table>
    </td>
    </tr>

    </tabLe>
    <% oRs.close
    set oRs = nothing
    set adoCon = nothing
    %>
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    20
    Rep Power
    0
    Code:
    <tr>
    <td class="a">&nbsp;</td>
    <td class="a">&nbsp;</td>
    <td class="a">&nbsp;/td>
    <td class="a">&nbsp;Total</td>
    <td class="a">&nbsp;<%=tCost%></td>
    </tr>
    You are missing the left bracket in the 4th line above.....
    it should read
    Code:
    <tr>
    <td class="a">&nbsp;</td>
    <td class="a">&nbsp;</td>
    <td class="a">&nbsp;</td>
    <td class="a">&nbsp;Total</td>
    <td class="a">&nbsp;<%=tCost%></td>
    </tr>
    But that should not be the cause of your problem.

    I notice this is VERY different from the code you provided in your original post.

    So I need to know just which lines of code are we actually working with ?
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2008
    Posts
    28
    Rep Power
    0
    Please ignore my original code. This is the modified code. It didn't calculate the total, but show the last row total only.
    For example:
    Row 1: $50
    Row 2: $100
    -----------
    Total: $100
    >>Total should be $150

    Here is the code:
    thanks.
    Code:
    <table width="960" align="center">
    <form method="Department">
    <tr height="25">
    <td class="a">&nbsp;Department Name</td>
    <td class="a">&nbsp;Student</td>
    <td class="a">&nbsp;Start</td>
    <td class="a">&nbsp;End</td>
    <td class="a">&nbsp;Cost</td>
    </tr>
    <% tCost =0
    While Not oRs.EOF %>
    <tr>
    <td><%=oRs("Department")%>&nbsp;</td>
    <td><%=oRs("Name")%>&nbsp;</td>
    <td><%=FormatDateTime(Month(oRs("StartDate")) & "/" & Day(oRs("StartDate")) & "/" & Year(oRs("StartDate")))%>&nbsp;</td>
    <td><%=FormatDateTime(Month(oRs("EndDate")) & "/" & Day(oRs("EndDate")) & "/" & Year(oRs("EndDate")))%>&nbsp;</td>
    <td <%=sRowStyle%>>$<%=oRs("Cost")%>&nbsp;</td>
    <% tCost = tCost + oRs("Cost") %>
    </tr>
    <% oRs.MoveNext %>
    <% WEND %>
    <tr>
    <td class="a">&nbsp;</td>
    <td class="a">&nbsp;</td>
    <td class="a">&nbsp;</td>
    <td class="a">&nbsp;Total</td>
    <td class="a">&nbsp;<%=tCost%></td>
    </tr>
    </table>
    </td>
    </tr>
    
    </tabLe>
    <% oRs.close
    set oRs = nothing
    set adoCon = nothing
    %>
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    20
    Rep Power
    0
    Well, if the actual code you are using is EXACTLY like you have provided here, then it SHOULD be working correctly.
    There is nothing wrong with the code you have provided here.
    It SHOULD produce what you want.
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2008
    Posts
    28
    Rep Power
    0
    I removed this: tCost =0

    and changed
    Code:
    <% tCost = tCost + oRs("Cost") %>
    to
    <%
    if isNumeric(trim( oRs("Cost") & " ")) then
    tCost = tCost + cdbl(oRs("Cost"))
    end if
    %>
    Then, it worked.

    Thanks.
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    20
    Rep Power
    0
    There was nothing wrong with initializing your total cost variable.
    In fact, that is what you really need to do.

    Making a test to see if the database record variable is not equal to blank is reasonable... but we had now way of knowing that the variable was a character string in your database definition. This is why we really need ALL the details to try to solve a problem.

    I'm glad you got it working.
  18. #10
  19. No Profile Picture
    Permanently Banned
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    3
    Rep Power
    0
    Such a great stuff dear thanks for sharing to all of you.

IMN logo majestic logo threadwatch logo seochat tools logo