Using VB to work out percentages

LeeSmith

Registered User.
Local time
Today, 15:49
Joined
Mar 17, 2006
Messages
27
Hi. Apologies if this question has been posted before, but I couldn't find it when I searched.

I'm trying to populate 4 fields with percentage amounts when a button is pressed. It appears to work fine when done as a number, but when I change the format of the 4 result fields to percentage it just shows zero for all. Below in italics is an example of my code.

Private Sub cmdEnd_Click()
On Error GoTo Err_cmdEnd_Click

ResultOne = NumberOne / (NumberOne + NumberTwo + NumberThree + NumberFour)
ResultTwo = NumberTwo / (NumberOne + NumberTwo + NumberThree + NumberFour)
ResultThree = NumberThree / (NumberOne + NumberTwo + NumberThree + NumberFour)
ResultFour = NumberFour / (NumberOne + NumberTwo + NumberThree + NumberFour)

Exit_cmdEnd_Click:
Exit Sub

Err_cmdEnd_Click:
MsgBox Err.Description
Resume Exit_cmdEnd_Click

End Sub


Any help with this would be appreciated.
 
You may have to multiple by 100 first. Or maybe set the decimals to display to 2. ?
 
That gave a % of 2500.00 when I tried with a simple one (four instances of 5)
 
Sorry - Just quessing at this point...

Maybe you need to wrap it in a val() function?

ken :)
 
you got this slight out of sink..
lets use your example
5 /(5 +5+5+5+5)= 5/25 but you want as a percentage so you are after 20% yes

right

toal your figures first with a little sub routine
Gtotal = one+two+three+four+five

now
ResultOne = NumberOne/gtotal *100
this will give you 0.20 * 100 = 20
then your field properites set to however many decimeils you require (2 would be good)

also don't forget a refresh option after each field update so that it recalulates

should work

regards
 
Hey Ken,
I got it , and for once I think my explaination was lsight more clear (even if you take into account the typo)
theres bound to be 1/2 dozen ways of doing this - but me I like to keep it simple .


g
 
I've set the properties to 2 decimal places, yet it still only gives me a whole number
 
what are the properties of the table underlying these fields or change the properties to general number and try - its a case of messing around with this

g
 
fixed and auto should work
just tried it myself
 

Users who are viewing this thread

Back
Top Bottom