Solved

Calculations giving incorrect results

Posted on 2015-02-22
7
65 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
Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Recently I finished a vbscript that I thought I'd share.  It uses a text file with a list of server names to loop through and get various status reports, then writes them all into an Excel file.  Originally it was put together for our Altiris server…
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…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

803 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