Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Handy way of summing up a lot of fields

This function is useful if you need to add up numbers from many fields

// ******************************************************************************** 
// Function:       FieldsSum                                                        
// Description:    Returns the sum of Fields with an incremental name               
//                 (ie Edit1,Edit2,Edit3,Edit4 etc)                                 
//                 Usage example:                                                   
//                 To perform the equivalent of                                     
//                 Edit1+Edit2+Edit3+Edit4                                          
//                 use the following:                                               
//                 js:FieldsSum("Edit",1,4,0)                                       
//                                                                                  
// Parameters:     Base field name As string                                        
//                 Starting number                                                  
//                 Last number                                                      
//                 Number of decimals to return                                     
// Returns:        String                                                           
// Special Logic:  Will only sum fields with valid numbers.                         
//                 Marks invalid fields(color & bold)                               
// ================================================================================ 
// Date       Version  Author               Description                             
// ---------- -------- -------------------- --------------------------------------- 
// 2018-11-30 1.0.0    Anders Forslund      Routine created                         
// ******************************************************************************** 
function FieldsSum(strBaseFieldName, intFieldStartNumber, intFieldEndNumber,intDecimals) {
  var dblsum = 0.0;
  var intCtrl = 0;
  var strValue = "";
  var strFieldName="";

  for (intCtrl = intFieldStartNumber; intCtrl <= intFieldEndNumber; intCtrl++) {
    try {
      // check that field exists. if the field does not exists, it will trigger the error check
      strFieldName = eval(strBaseFieldName + intCtrl + "\.FieldName");
      // get text from field
      strValue = eval(strFieldName + "\.Text");      
      // check for empty string
      if (strValue === "") {
        // do nothing
      } else {
        // check for valid number
        if (isNumber(strValue)) {
          dblsum = dblsum + Number(strValue);
          // mark it ok
          eval(strBaseFieldName + intCtrl).FillColor = rgb(0, 0, 0);
          eval(strBaseFieldName + intCtrl).FontBold = false;
        } else {
          // alert user for faulty number format          
          eval(strBaseFieldName + intCtrl).FillColor = rgb(255, 0, 0);
          eval(strBaseFieldName + intCtrl).FontBold = true;
        }
      }
    } catch (e) {
      // something went wrong
      DebugMsg("Error in FieldsSum:" + e.message);
    } finally {
      //do nothing
    }
  }
  // make sure to return it with a dot(.) as string
  return String(dblsum.toFixed(intDecimals));
}

Leave a Comment