Solved

Calculations giving incorrect results

Posted on 2015-02-22
7
69 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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

Question has a verified solution.

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

This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
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…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

734 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