Option Compare Database   'Use database order for string comparisons


'  Using CStr to convert incoming numeric values will assure there are
'  no commas or leading zeros etc etc.
Function CheckNumSpelledOut$ (numin$)
    numin$ = LTrim$(RTrim$(numin$))

    If InStr(numin$, ".") = 0 Then
        number$ = numin$
    Else
        number$ = Left$(numin$, InStr(1, numin$, ".") - 1)
    End If

'
'  With CStr used to pass numbers, numbers ending in 0 or 00 will
'truncate the last 0.  So, we cannot just take the last 2 positions
'on the right and take them to be the cents.  We have to find the
'decimal point, first, then extract everything to the right of it
'and convert it to 2 positions.
'
'    cents$ = Right$(numin$, 2)
    If InStr(1, numin$, ".") = 0 Then   'No decimal point or cents
        cents$ = "00"
    ElseIf (Len(numin$) - InStr(1, numin$, ".")) >= 2 Then
        cents$ = Mid$(numin$, InStr(1, numin$, ".") + 1, 2)
    Else
        cents$ = Mid$(numin$, InStr(1, numin$, ".") + 1, 1) & "0"
    End If

    'Debug.Print "number: "; number$

    numlen% = Len(number$)
    hunpart$ = ""
    milpart$ = ""
    thopart$ = ""

    Select Case numlen%
        Case 0
        Case 1 To 3
            hunpart$ = Right$(number$, numlen%)
        Case 4 To 6
            hunpart$ = Right$(number$, 3)
            thopart$ = Left$(number$, numlen% - 3)
        Case 7 To 9
            milpart$ = Left$(number$, numlen% - 6)
            thopart$ = Mid$(number$, numlen% - 5, 3)
            hunpart$ = Right$(number$, 3)
        Case Else
            MsgBox "Design error: number too large to convert"
            CheckNumSpelledOut = "Bad Input"
            Exit Function
    End Select
    
    'Debug.Print "millions: "; milpart$
    'Debug.Print "thousands: "; thopart$
    'Debug.Print "hundreds: "; hunpart$

    If hunpart$ = "000" Then
        huntext$ = ""
    Else
        huntext$ = SpellHundred(hunpart$)
    End If

    If thopart$ = "000" Then
        thotext$ = ""
    Else
        thotext$ = SpellHundred(thopart$)
    End If

    If milpart$ = "000" Then
        miltext$ = ""
    Else
        miltext$ = SpellHundred(milpart$)
    End If

    ftext$ = ""
    If miltext$ <> "" Then
        If miltext$ <> "Zero" Then
            ftext$ = miltext$ + " Million "
        End If
    End If

    If thotext$ <> "" Then
        If thotext$ <> "Zero" Then
            ftext$ = ftext$ + thotext$ + " Thousand "
        End If
    End If

    If huntext$ <> "" Then
        ftext$ = ftext$ + huntext$
    End If
    
    'ftext$ = ftext$ + " Dollars and " + cents$ + " Cents"
    If ftext$ <> "" Then
        ftext$ = ftext$ + " and " + cents$ + "/100 dollars"
    Else
        ftext$ = "Zero Dollars and " & cents$ & "/100 dollars"
    End If
    CheckNumSpelledOut = ftext$


End Function


Private Function SpellHundred$ (numin$)

    Dim digs As String
    Dim tens As String
    Dim huns As String
    Dim numlen As Integer

numlen = Len(numin$)
huns = ""
tens = ""
digs = ""

If numlen = 3 Then
    Select Case Left$(numin$, 1)
        Case "0": huns = ""
        Case "1": huns = "One Hundred"
        Case "2": huns = "Two Hundred"
        Case "3": huns = "Three Hundred"
        Case "4": huns = "Four Hundred"
        Case "5": huns = "Five Hundred"
        Case "6": huns = "Six Hundred"
        Case "7": huns = "Seven Hundred"
        Case "8": huns = "Eight Hundred"
        Case "9": huns = "Nine Hundred"
    End Select

    If Right$(numin$, 2) = "00" Then
        SpellHundred = huns
        Exit Function
    End If
End If

If numlen >= 2 Then
    Select Case Mid$(numin$, Len(numin$) - 1, 1)
        Case "0": tens = ""
        Case "1": tens = "Ten"
        Case "2": tens = "Twenty"
        Case "3": tens = "Thirty"
        Case "4": tens = "Forty"
        Case "5": tens = "Fifty"
        Case "6": tens = "Sixty"
        Case "7": tens = "Seventy"
        Case "8": tens = "Eighty"
        Case "9": tens = "Ninety"
    End Select
End If

If numlen >= 1 Then
    Select Case Right$(numin$, 1)
        Case "0", " ", "": digs = ""
        Case "1": digs = "One"
        Case "2": digs = "Two"
        Case "3": digs = "Three"
        Case "4": digs = "Four"
        Case "5": digs = "Five"
        Case "6": digs = "Six"
        Case "7": digs = "Seven"
        Case "8": digs = "Eight"
        Case "9": digs = "Nine"
    End Select
End If

If Len(numin$) < 2 Then
    SpellHundred = digs
    Exit Function
End If

Select Case tens
    Case "Ten"
        Select Case digs
            Case "Zero", "", " ": temp$ = "Ten"
            Case "One": temp$ = "Eleven"
            Case "Two": temp$ = "Twelve"
            Case "Three": temp$ = "Thirteen"
            Case "Four": temp$ = "Fourteen"
            Case "Five": temp$ = "Fifteen"
            Case "Six": temp$ = "Sixteen"
            Case "Seven": temp$ = "Seventeen"
            Case "Eight": temp$ = "Eighteen"
            Case "Nine": temp$ = "Nineteen"
        End Select
    Case Else
        If digs = "" Then
            temp$ = tens
        Else
            temp$ = tens + "-" + digs
        End If
End Select

Select Case numlen
    Case 0
        SpellHundred = ""
    Case 1
        SpellHundred = digs
    Case 2
        SpellHundred = temp$
    Case 3
        If huns = "" Then
            If tens = "" Then
                If digs = "" Then
                    SpellHundred = ""
                Else
                    SpellHundred = digs
                End If
            Else
                SpellHundred = temp$
            End If
        Else
            If tens = "" Then
                If digs = "" Then
                    SpellHundred = huns
                Else
                    SpellHundred = huns & " " & digs
                End If
            Else
                SpellHundred = huns & " " & temp$
            End If
        End If
End Select

End Function

