Help with Function

Results 1 to 5 of 5

Thread: Help with Function

  1. #1
    JSNewbie Guest

    Default Help with Function

    I am trying to write a function to go through 2 recordsets and add up the results. so far this is what i have:<BR><BR>&#060;%<BR>function getTotals(rsname1, rsname2) {<BR>while ((!rsname1.EOF) && (!rsname2.EOF)) {<BR>var num1 = (rsname1.Fields.Item("TIMESPENT").Value)<BR>var num2 = (rsname2.Fields.Item("INTHOURS").Value)<BR>return (Sum(num1 / num2))<BR> rsname1.MoveNext(); rsname2.MoveNext();<BR> }<BR>}<BR>%&#062;<BR><BR>And then I plug in the recordset names when I call the function:<BR><BR>&#060;%= getTotals(rstsvolach, rsihach) %&#062;<BR><BR>However, it doesnt seem to grab the rs names - can anyone help?<BR><BR>Thank you!

  2. #2
    Joel N Guest

    Default RE: Help with Function

    Just off the top of my head, without running your code to check for other problems I do see one error. When you do your:<BR><BR>return(Sum(num1 / num2))<BR><BR>you are blasting out of the function. The MoveNext() methods never happen.<BR><BR><BR>If you want to get the total of all the num1&#039;s divided by the num2&#039;s try something like this:<BR><BR><BR>&#060;%<BR>function getTotals(rsname1, rsname2) {<BR>var total;<BR>while ((!rsname1.EOF) && (!rsname2.EOF)) {<BR>var num1 = (rsname1.Fields.Item("TIMESPENT").Value);<BR>var num2 = (rsname2.Fields.Item("INTHOURS").Value);<BR>total += (num1 / num2);<BR>rsname1.MoveNext(); rsname2.MoveNext();<BR>}<BR>return total;<BR>}<BR>%&#062;<BR><BR><BR>This assumes that the values in the database are numeric. If they are strings you may have to use the parseFloat() or parseInt() methods to get the math right.<BR><BR><BR>HTH<BR>

  3. #3
    JSNewbie Guest

    Default RE: Help with Function

    That makes sense...I tried it though and it still doesn&#039;t return a value - the only thing I can think is that it isnt recognizing the variables when I call the function - do I have to do something special to get it to see that they are variables and it should replace the rsname1, rsname2 with rstsvolach, rsihach?<BR><BR>Thank you!!

  4. #4
    Join Date
    Dec 1969

    Default RE: Help with Function

    Unless you&#039;re using a SHAPE statement in SQL, I think this will be sufficient:<BR><BR>var num1 = rsname1(&#039;TIMESPENT&#039;)<BR><BR>(presuming TIMESPENT is a field (or alias) returned by SQL)<BR>

  5. #5
    JSNewbie Guest

    Default If it isn't one thing...

    I changed it and that does appear to work just as well, thank you!<BR>Now I am getting a result however the result is really strange and I am hoping someone will have seen this before. Here is the new improved version of the function:<BR>&#060;% <BR>function getTotals(rsname1, rsname2) { <BR>var total; <BR>rsname1.MoveFirst();<BR>rsname2.MoveFirst();<B R>while ((!rsname1.EOF) && (!rsname2.EOF)) { <BR>var num1 = (rsname1(&#039;TIMESPENT&#039;)); <BR>var num2 = (rsname2(&#039;INTHOURS&#039;)); <BR>total += (num1 / num2); <BR>rsname1.MoveNext(); rsname2.MoveNext(); <BR>} <BR>return total; <BR>} <BR>%&#062;<BR><BR>Now the screwy thing is the result I get is always -1.#IND <BR>Anyone have any words of wisdom for this?<BR><BR>Thank you again!!

Posting Permissions

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