Solved

Calculations giving incorrect results

Posted on 2015-02-22
7
70 Views
Last Modified: 2015-02-22
I have the following code on an intranet page
<SCRIPT Language="VBScript">
Sub CheckChg(currentchg,Pme)
dim newrate
dim payrate
dim bflag
dim oflag

bflag=document.getElementById("BMFlag").checked
oflag=document.getElementById("OPSFlag").checked

If Pme="AAA" then
  payrate= document.getElementById("AAA_P").value
  newrate=payrate+(payrate * 0.2)
msgbox (currentchg.value < newrate) & "  :  " & currentchg.value & "  <  " & newrate
  If currentchg.value <  newrate  then     
    currentchg.value=newrate
  end if
end if

end sub
</script>

Open in new window

The user enters a pay rate on the form, and the code is meant to calculate the charge rate at 20% above the pay rate.  eg. a pay rate of 21 gives a charge rate as 25.2 which is correct, and computes correctly on the form.  

The user should be able to amend the charge rate upwards, but not downwards unless either the bflag or oflag is checked.  In the above example if I change the 25.2 to 23, the msgbox (put in for testing purposes) tells me that 23 is less than 25.2 is false and it doesn't change the rate.

What am I doing wrong?
0
Comment
Question by:jdthedj
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 65

Expert Comment

by:RobSampson
ID: 40624625
Hi, I think I have your logic right, based on the field names.  See if this works.

Regards,

Rob.

<SCRIPT Language="VBScript">
Sub CheckChg(currentchg,Pme)
dim newrate
dim payrate
dim bflag
dim oflag

bflag=document.getElementById("BMFlag").checked
oflag=document.getElementById("OPSFlag").checked

If Pme="AAA" then
  payrate= document.getElementById("AAA_P").value
  newrate=payrate+(payrate * 0.2)
  If currentchg.value < newrate Then
	MsgBox (currentchg.value < newrate) & "  :  " & currentchg.value & "  <  " & newrate
    currentchg.value=newrate
  ElseIf bflag = True Or oflag = True Then
  	MsgBox "An override flag is set. Setting new rate."
  	currentchg.value=newrate
  end if
end if

end sub
</script>

Open in new window

0
 
LVL 3

Author Comment

by:jdthedj
ID: 40624715
Thanks for the reply Rob - I'm not concerned about the oflag and bflag for now. Leaving them out (as in my code above) shows that if I type in 21 as the pay rate, the charge rate calculates correctly to 25.2

If I change the 25.2 to 23, the charge rate automatically reverts to 25.2 (the desired effect), but if I change the charge rate to 28 (which is above 25.2) it still reverts to 25.2, but I want it to stay at 28.
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 40624745
OK, we can try typecasting the values to the Currency data type:
<SCRIPT Language="VBScript">
Sub CheckChg(currentchg,Pme)
dim newrate
dim payrate
dim bflag
dim oflag

bflag=document.getElementById("BMFlag").checked
oflag=document.getElementById("OPSFlag").checked

If Pme="AAA" Then
	payrate= document.getElementById("AAA_P").value
	newrate=ccur(payrate+(payrate * 0.2))
	MsgBox "Test for currentchg.value < newrate:  " & ccur(currentchg.value) & "  <  " & ccur(newrate)
	If ccur(currentchg.value) <  ccur(newrate) then     
		currentchg.value=newrate
	end if
end if

end sub
</script>

Open in new window

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Author Comment

by:jdthedj
ID: 40624757
Sorry - makes no difference
0
 
LVL 65

Accepted Solution

by:
RobSampson earned 500 total points
ID: 40624817
OK, let's throw in some (hopefully) more meaningful messages at each branch, so you can see what might be going on.

Regards,

Rob.

<SCRIPT Language="VBScript">
Sub CheckChg(currentchg,Pme)
dim newrate
dim payrate
dim bflag
dim oflag

bflag=document.getElementById("BMFlag").checked
oflag=document.getElementById("OPSFlag").checked

If Pme="AAA" Then
	payrate= document.getElementById("AAA_P").value
	newrate=ccur(payrate+(payrate * 0.2))
	If ccur(currentchg.value) < ccur(newrate) Then
		MsgBox "Currentchg: " & ccur(currentchg.value) & vbCrLf & "Newrate: " & ccur(newrate) & vbCrLf & _
			ccur(currentchg.value) & " is less than " & ccur(newrate) & vbCrLf & _
			"Setting currentchg.value to " & ccur(newrate)
		currentchg.value=newrate
	Else
		MsgBox "Currentchg: " & ccur(currentchg.value) & vbCrLf & "Newrate: " & ccur(newrate) & vbCrLf & _
			ccur(currentchg.value) & " is greater than " & ccur(newrate) & vbCrLf & _
			"Not setting currentchg.value to " & ccur(newrate)
		'currentchg.value=newrate		
	End If
end if

end sub
</script>

Open in new window

0
 
LVL 3

Author Closing Comment

by:jdthedj
ID: 40624909
Thanks Rob - seems to work OK now.  It was not recognising the onBlur if the value was null to begin with, so not calling the code every time.  The Ccur seems to have done the trick because even defaulting to 0 wouldn't give the right answer until I added ccur
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 40624928
Great!  Thanks for the grade.

Rob.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Well hello again!  Glad to see you've made it this far without giving up.  In this, the fourth installment of my popular series, I'm going to cover functions and subroutines, what they are, and why they are useful.  Just in case you stumbled onto th…
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question