I am trying to get a grand total from a shopping cart page and am not sure how to do that. I have the total that loops through each section but I want to display the grand total from all of those sections at the bottom of the page. Can someone offer assistance on how to do this. Here is my code:

dim totalPrice(20)  //The total price on any given subform  //CONFIG - change this to the maximum number of product types you ever expect to have.
    	dim iQtys(20, 50)  //CONFIG - Change this to the maximum number of items you ever expect to have on one order form.
    	dim emailTo, emailFrom, emailSubject
    	dim grandTotal(20)

    	if Request.Form("btnCalculate.x") <> 0 or Request.Form("btnCalculate.y") <> 0  then
    		isPostback = 1 'cInt(Request.Form("btnCalculate"))
    		
    		
    		for iCntr2 = 1 to Request.Form("NumForms")
    		
    			for iCntr = 1 to Request.Form("NumElements" & iCntr2)
    		
    				iQtys(iCntr2, iCntr) = CInt(Request.Form("Qty" & iCntr2 & "_" & iCntr))
    				totalPrice(iCntr2) = totalPrice(iCntr2) + (Ccur(Request.Form("UnitPrice" & iCntr2 & "_" & iCntr)) * CInt(Request.Form("Qty" & iCntr2 & "_" & iCntr)))
    		
	    		next
	    		
	    	next
    		
    	    		
    	end if

	totalPrice(iCntr2) = totalPrice(iCntr2) + (Ccur(Request.Form("UnitPrice" & iCntr2 & "_" & iCntr)) * CInt(Request.Form("Qty" & iCntr2 & "_" & iCntr)))

    	Response.write "<TR><TD align=left colspan=" & oProdInstances.Fields.Count - 2 & ">Grand Total:  " & FormatCurrency(grandTotal(iForm), 2) & "</TD>"

Dani AI

Generated

Quick diagnosis for : the per-section subtotals are being built, but a true grand total is never computed or output. There’s also a stray accumulation line after the loops and the output uses grandTotal(iForm) (where iForm isn’t defined). Classic ASP/VBScript will also raise errors if Request.Form values are empty strings, and arrays default to 0-based unless you set Option Base 1 — both common sources of off‑by‑one or conversion errors.

A straightforward, robust fix is to keep a scalar grandTotal and add each section’s subtotal to it as each inner loop finishes. Use a safe numeric conversion (Val or a small helper) so empty fields don’t throw errors. Example approach:

Function SafeNum(s)
  SafeNum = Val(s & "")
End Function

Dim numForms, subTotals(), grandTotal
numForms = CInt(Val(Request.Form("NumForms")))
ReDim subTotals(numForms)
grandTotal = 0

For f = 1 To numForms
  subTotals(f) = 0
  Dim numEl, e, qty, unit
  numEl = CInt(Val(Request.Form("NumElements" & f)))
  For e = 1 To numEl
    qty = CInt(Val(Request.Form("Qty" & f & "_" & e)))
    unit = SafeNum(Request.Form("UnitPrice" & f & "_" & e))
    subTotals(f) = subTotals(f) + (unit * qty)
  Next
  grandTotal = grandTotal + subTotals(f)
Next

Response.Write "<tr><td colspan=""" & (oProdInstances.Fields.Count - 2) & """>Grand Total: " & FormatCurrency(grandTotal, 2) & "</td></tr>"

Practical troubleshooting notes:

  • Remove the duplicate accumulation line that appears after the End If (it’s likely leftover and will double-count or error).
  • Initialize each section subtotal to 0 before the inner loop.
  • Use Val() or a small helper to guard against empty/non-numeric form fields instead of calling CInt/CCur directly on blanks.
  • If your code indexes from 1, either use Option Base 1 or dimension arrays with ReDim using the exact number of forms to avoid out‑of‑range access.
  • If the form uses an image button, check presence of the submit (e.g., Request.Form("btnCalculate.x") <> "") rather than assuming numeric coordinates.

For ’s code the minimal change is to remove the stray line and add a scalar grandTotal accumulation (or a second loop summing the subtotal array) as shown; that will produce the displayed grand total at the bottom.

Anyone with any ideas

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.