Simple Maths regarding division?...

connexion

Registered User.
Local time
Today, 20:37
Joined
Jul 30, 2003
Messages
72
Hi there,

Im putting a whole number representing gift voucher amount into a text box and want to display the correct quantity and denomination of vouchers required to make up the total amount required in 4 other text boxes.

Box1 = "Claimed"
Box2 = "20's"
Box3 = "10's"
Box4 = "5's"
Box5 = "1's"

i.e. if the amount is £39
this needs 1 x £20, 1 x £10, 1 x £5, 4 x £1 vouchers to fulfill the order.
Is there a simple/slick way of coding this, starting with the highest denomination to see if the whole number is divisible by that, then working down through the other denominations dealing with any remainder as you go, to see if that is divisible by a lesser denomination?

As this is only dealing with whole numbers, there won't be any fractions left over so they don't need to be dealt with, i'm just keen to see how complex or simple the function would be.

Thanks
Vince
 
Assuming the text boxes are named txtClaimed, txt20s, txt10s, txt5s and txt1s,
try this code on the form e.g. in the On Click event of a command button.
Code:
   Dim i As Integer
   
   i = Me.txtClaimed
   Me.txt20s = i \ 20
   Me.txt10s = (i Mod 20) \ 10
   Me.txt5s = ((i Mod 20) Mod 10) \ 5
   Me.txt1s = ((i Mod 20) Mod 10) Mod 5

.
 
Thanks for that

Hi Jon,

Thanks for that, just goes to show how simple things can be....when you know what you're doing!

Works a treat!

Vince
 
For those of you who are not aware, there are two division symbols supported by VBA - "/" which we all know and love and "\" which returns only the whole number portion of the quotient.
 
Glad I found this. I was just today looking for how VBA handles Quotients. Looks like I can also get Remainders with Mod directly in VBA, too?
 

Users who are viewing this thread

Back
Top Bottom