If you appreciate these tips, please consider a small donation:

 General Tip Example of Determining Change Denominations in Visual Basic Code Tested with VB6 Courtesy of Tim Thomas This example calculates what the breakdown of change would be for a specified amount in dollars and cents. For example, 25.78 would be 25 dollars, 3 quarters and 3 pennies. It assumes you are entering a currency amount with two decimal places, and that all standard coin denominations are available. Variations are left as an exercise for the reader. 1 - Start a new, standard VB project. 2 - Add a command button to the form. 3 - Add a textbox to the form and name it txtmon. 4 - Place the following code in the General Declarations section of the form code: Dim intcoin As Currency Dim intquarter As Integer Dim intdime As Integer Dim intnickel As Integer Dim intpenny As Integer Public Sub subGetQuarter()  If intcoin > 75 Then     intquarter = 3     intcoin = intcoin - 75  ElseIf intcoin > 50 Then     intquarter = 2     intcoin = intcoin - 50  ElseIf intcoin > 25 Then     intquarter = 1     intcoin = intcoin - 25  Else        intquarter = 0  End If End Sub Public Sub subGetDimes()  If intcoin > 20 Then     intdime = 2     intcoin = intcoin - 20  ElseIf intcoin > 10 Then     intdime = 1     intcoin = intcoin - 10  Else     intdime = 0  End If End Sub Public Sub subGetNickels()  If intcoin > 5 Then     intnickel = 1     intcoin = intcoin - 5  End If End Sub Public Sub subGetPennies()  If intcoin = 4 Then     intpenny = 4     intcoin = intcoin - 4  ElseIf intcoin = 3 Then     intpenny = 3     intcoin = intcoin - 3  ElseIf intcoin = 2 Then     intpenny = 2     intcoin = intcoin - 2  ElseIf intcoin = 1 Then     intpenny = 1     intcoin = intcoin - 1  End If End Sub Public Sub subInvalidNum() 'This sub puts out msgbox that number is not a valid one ' with 2 decimal points MsgBox "Not a valid number with decimal points, please reenter." txtmon = "" txtmon.SetFocus End Sub 5 - Place the following code in the command button's click event: Dim intmoney As Currency Dim strmoney As String Dim myresult As Currency Dim intlen As Integer 'length of string Dim intdollars As Currency Dim strIsDecimal As String On Error GoTo errTrap: ' I am doing it so it must have a decimal point ' and 2 places or it fails. ' grab amount  strmoney = Trim(txtmon.Text) ' grab length of string  intlen = Len(strmoney)  intmoney = CCur(txtmon.Text)  If intlen < 3 Then  'if length of number less then 3 then not valid,  'because need at least 3 points with decimal     subInvalidNum     Exit Sub  End If ' making sure the appropriate spot is a decimal  strIsDecimal = Mid(strmoney, intlen - 2, 1) ' if this spot is not a decimal, we prompt user to start again  If strIsDecimal <> "." Then     subInvalidNum     Exit Sub  End If ' Now we get Dollar amount  If intlen > 2 And Left(strmoney, intlen - 2) = "." Then     intdollars = 0  Else     intdollars = Left(strmoney, intlen - 2)  End If '************* ' Now for quarters ' get starting coin amount  intcoin = CInt(Right(strmoney, 2))  subGetQuarter '****** ' dimes  subGetDimes '***** 'Nickels  subGetNickels '****** 'Pennies  subGetPennies ' lets give the answer.. MsgBox "Dollars = " & intdollars & vbCrLf & "Quarters = " & intquarter & vbCrLf & "Dimes = " & intdime & vbCrLf & "Nickels = " & intnickel & vbCrLf & "Pennies = " & intpenny Exit Sub errTrap:  If err.Number = 13 Then     MsgBox "Please enter valid number"     txtmoney = ""     Exit Sub  Else         MsgBox err.Number & err.Description  End If 5 – Run the program, enter a number in the textbox and test. Note that the code could also be placed in a routine callable from anywhere it might be needed in a program.

[ Back To The Top ]

Contact: web@xtremecomp.com
All contents copyright XTreme Computing unless noted otherwise.