Convert

May 7, 2008

Option Explicit
Dim BigOnes(9) As String
Dim SmallOnes(19) As String
  

Dim Dollars As String
Dim Cents As String
Dim Words As String
Dim Chunk As String
Dim Digits As Integer
Dim LeftDigit As Integer
Dim RightDigit As Integer
Public Sub ParseChunk()
   
    Digits = Mid(Chunk, 1, 1)
    If Digits > 0 Then
        Words = Words & ” ” & SmallOnes(Digits) & ” Hundred”
    End If
    Digits = Mid(Chunk, 2, 2)
    If Digits > 19 Then
        LeftDigit = Mid(Chunk, 2, 1)
        RightDigit = Mid(Chunk, 3, 1)
        Words = Words & ” ” & BigOnes(LeftDigit)
        If RightDigit > 0 Then
            Words = Words & ” ” & SmallOnes(RightDigit)
        End If
    Else
        If Digits > 0 Then
            Words = Words & ” ” & SmallOnes(Digits)
        End If
    End If
End Sub
   
Private Sub Command1_Click()

    Text1.Text = Format(Text1.Text, “000000.00″)
    Dollars = Left(Text1.Text, 6)
    Cents = Right(Text1.Text, 2)
   
    Words = “”

    If Dollars > 999999 Then
        Text2.Text = “Dollar amount is too large”
    ElseIf Dollars = “” Then
        Text2.Text = “Please enter an amount”
        Exit Sub
    End If
   

    If Dollars = 0 Then
        Words = “Zero”
    Else
   

        Chunk = Left(Dollars, 3)
        If Chunk > 0 Then
            ParseChunk
            Words = Words & ” Thousand”
        End If
       

        Chunk = Right(Dollars, 3)
        If Chunk > 0 Then
            ParseChunk
        End If
    End If

    If Cents = 0 Then Cents = “No”
    Words = Words & ” and ” & Cents & “/100″
    Text2.Text = Words
    Exit Sub
   
End Sub

Private Sub Command2_Click()
Text1.Text = “”
Text2.Text = “”

End Sub

Private Sub Form_Load()
‘Populate the arrays
    BigOnes(1) = “Ten”
    BigOnes(2) = “Twenty”
    BigOnes(3) = “Thirty”
    BigOnes(4) = “Forty”
    BigOnes(5) = “Fifty”
    BigOnes(6) = “Sixty”
    BigOnes(7) = “Seventy”
    BigOnes( 8) = “Eighty”
    BigOnes(9) = “Ninety”
   
    SmallOnes(1) = “One”
    SmallOnes(2) = “Two”
    SmallOnes(3) = “Three”
    SmallOnes(4) = “Four”
    SmallOnes(5) = “Five”
    SmallOnes(6) = “Six”
    SmallOnes(7) = “Seven”
    SmallOnes( 8) = “Eight”
    SmallOnes(9) = “Nine”
    SmallOnes(10) = “Ten”
    SmallOnes(11) = “Eleven”
    SmallOnes(12) = “Twelve”
    SmallOnes(13) = “Thirteen”
    SmallOnes(14) = “Fourteen”
    SmallOnes(15) = “Fifteen”
    SmallOnes(16) = “Sixteen”
    SmallOnes(17) = “Seventeen”
    SmallOnes(1 8) = “Eighteen”
    SmallOnes(19) = “Nineteen”
End Sub