Solved

Calculations giving incorrect results

Posted on 2015-02-22
7
61 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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
I met Paul Devereux (@pdevereux) today when I responded to his tweet asking “Anybody know how to automate adding files from disk to a folder in #outlook  ?”.  I replied back and told Paul that using automation, in this case scripting, to add files t…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

760 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now