Tricky Math Problem

# Thread: Tricky Math Problem

1. Senior Member
Join Date
Dec 1969
Posts
228

## Tricky Math Problem

This code results in multiple rows of data in a form, and inserts multiple rows of data into a table.<BR>My problem: I need to multiply the unitprice times the quantity, and then add the results for each row altogether and insert the result into my table.<BR><BR>I have been experimenting, but so far I&#039m not even coming close.<BR><BR><BR>&#060;% y=1<BR>rs.movefirst<BR>do while not rs.eof%&#062;<BR><BR>&#060;input class="right" type="text" name="item" value="&#060;%=FormatNumber((y), 0)%&#062;" size="1"&#062;<BR><BR>&#060;input class="right" type="text" name="tagnumber&#060;%=y%&#062;" value="&#060;%=rs("tagnumber")%&#062;" size="7"&#062;<BR><BR>&#060;input class="center" type="text" name="equipmentdesc&#060;%=y%&#062;" value="&#060;%=rs("equipmentdesc")%&#062;" size="30"&#062;&#060;/td&#062;<BR><BR>&#060;input class="right" type="text" name="unitprice" value="&#060;%=FormatCurrency(rs("unitprice"), 2)%&#062;" size="8"&#062;<BR><BR>&#060;input class="right" type="text" name="quantity&#060;%=y%&#062;" value="&#060;%=rs("quantity")%&#062;" size="3"&#062;<BR><BR>&#060;input class="right" type="text" name="itemtotal" value="&#060;%=FormatCurrency((rs("unitprice")*rs( "quantity")), 2)%&#062;" size="15<BR>(Note: here I am only multiplying the unitprice and quantity for each row. I have no clue as to how add these together)<BR> <BR>&#060;% y=y+1<BR>rs.movenext<BR>loop%&#062;

2. jd
Senior Member
Join Date
Dec 1969
Posts
850

## RE: Tricky Math Problem

Right after your Y=Y+1 line, couldn&#039t you do something like this?(this is simplified and contains no error/type checking)<BR><BR>Ttl = Ttl + (rs("quantity") * rs("unitprice"))<BR><BR>Then, when you&#039re done with your loop, the variable TTL will contain the total.

3. Senior Member
Join Date
Dec 1969
Posts
228

## RE: Tricky Math Problem

That looked really good, but in my experiments<BR><BR>Ttl = Ttl + (rs("quantity") * rs("unitprice")) results in page cannot be displayed<BR><BR>and<BR><BR>Ttl = (rs("quantity") * rs("unitprice")) just gives the last value.<BR><BR>and <BR><BR>Ttl = (rs("quantity") * rs("unitprice"))<BR>Total = Ttl + (rs("quantity") * rs("unitprice"))<BR>Ttl gives an erroneous result and (21+5=32)?<BR>Total just gives the last value<BR><BR>There has to be a way to calculate this.<BR><BR><BR>

4. jd
Senior Member
Join Date
Dec 1969
Posts
850

## RE: Tricky Math Problem

Nope. I stand by this. It will work. And I do not know what &#039page cannot be displayed&#039 means. Surely you must have a more explanatory error message?<BR><BR>Let&#039s add the data typing:<BR><BR>Ttl = Ttl + (cdbl(rs("quantity")) * cdbl(rs("unitprice")))<BR><BR><BR>

5. Senior Member
Join Date
Dec 1969
Posts
228

## RE: Tricky Math Problem

I pasted in your line of code, but still it gives only the result for the quantity * unit price for the last row.<BR>&#060;% y=y+1<BR> Ttl = Ttl + (cdbl(rs("quantity")) * cdbl(rs("unitprice")))<BR> rs.movenext<BR> loop%&#062;<BR> <BR>When I try ttl = ttl by itself, of course I get 0, so I think that is the weak link here?

6. jd
Senior Member
Join Date
Dec 1969
Posts
850

## Makes no sense.

So you are saying that this code shows a result for Ttl that only includes the last record? Is there more than one record?<BR><BR>&#060;% y=y+1<BR>Ttl = Ttl + (cdbl(rs("quantity")) * cdbl(rs("unitprice")))<BR>rs.movenext<BR>loop<BR>r esponse.write "The total was " & Ttl<BR><BR>%&#062;<BR><BR>

7. Senior Member
Join Date
Dec 1969
Posts
228

## Suddenly, it is makes perfect sense!

There are one or more records for the code. But this seems to be work if I put your code before the y=y+1!!!<BR><BR>&#060;%Ttl = Ttl + (cdbl(rs("quantity")) * cdbl(rs("unitprice")))<BR>y=y+1<BR>rs.movenext<BR> loop<BR> %&#062;

#### Posting Permissions

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