Trouble with the total value of a subform.

Trouble with the total value of a subform.

Hello,

I am working in a purchase order form with the following structure.

Purchase order:

date - date
company - text
provider - text
email - email
subform 
total order cost - currency

Subform (in purchase order):

reference - lookup to product catalog
description - text
cost - currency
IVA - number
cost plus IVA - currency 
quantity - number
final cost - currency

I wrote this code in the subform_quantity on user input section.

if (row.cost_plus_IVA  !=  null)
{
    row.final_cost = (row.cost_plus_IVA  *  row.quantity);
}
tot = 0.0;
for each row in subform
{
    tot = (tot  +  row.cost_plus_IVA  *  row.quantity);
}
input.total_order_cost = tot;

I do not know what the problem is. It works perfect with the first row, all values are fetched properly, all the operations are done properly and the total order cost is shown. But when I want to add a second row It fails and I get this message "Invalid value found fieldName: total_order_cost".

How can I fix this?

Any help will be very very appreciated.