Solved

Calculations giving incorrect results

Posted on 2015-02-22
7
67 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
  • 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Hello again, all.  For those of you that have been following along, you'll know that this is my third article on this topic (though it is not Part III).  This article is sort of remedial, and probably the topic with which I should have started the s…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

828 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