Good day Sir/Maam.. im new here. i have a project in MS Access 2003 that will tanslate numbers into words... is there a code using Asc? to this program... im sorry im newbie in programming.. thanks a lot..

Recommended Answers

All 7 Replies

I think this is what you are looking for...

'\ This function will convert only round numbers. Just paste the code as it in a fomr.
'\ Place one Textbox named "text1" and one command Button named "Command1"

Option Explicit
Dim sUnits(0 To 20) As String
Dim sTens(1 To 10) As String
Dim strIdent(0 To 4) As String
Dim iLenOfInputNum As Integer, sTempWord As String


Code

Private Sub populatewords()
'\ First of all i am populating the Words in an array
    sUnits(1) = "One"
    sUnits(2) = "Two"
    sUnits(3) = "Three"
    sUnits(4) = "Four"
    sUnits(5) = "Five"
    sUnits(6) = "Six"
    sUnits(7) = "Seven"
    sUnits(8) = "Eight"
    sUnits(9) = "Nine"
    sUnits(10) = "Ten"
    sUnits(11) = "Eleven"
    sUnits(12) = "Tweleve"
    sUnits(13) = "Thirteen"
    sUnits(14) = "Fourteen"
    sUnits(15) = "Fifteen"
    sUnits(16) = "Sixteen"
    sUnits(17) = "Seventeen"
    sUnits(18) = "Eighteen"
    sUnits(19) = "Nineteen"
    sTens(1) = "Ten"
    sTens(2) = "Twenty"
    sTens(3) = "Thirty"
    sTens(4) = "Fourty"
    sTens(5) = "Fifty"
    sTens(6) = "Sixty"
    sTens(7) = "Seventy"
    sTens(8) = "Eighty"
    sTens(9) = "Ninety"
    strIdent(1) = "Hundred"
    strIdent(2) = "Thousand"
    strIdent(3) = "Lac"
    strIdent(4) = "Crore"
End Sub
Public Function sNumToWords(strNumber As String) As String
    Call populatewords
    sTempWord = ""
    strNumber = CDbl(strNumber)
    iLenOfInputNum = Len(strNumber)
    If iLenOfInputNum < 10 Then
        sTempWord = Crores(strNumber)
        sNumToWords = sTempWord
    Else
        sNumToWords = "Unable to convert the number to Words, since the number is bigger than 10 crores"
    End If
End Function

Private Function TenthPos(number As String) As String
Dim tempint As Integer
Dim sp As String
tempint = 0: sp = ""
tempint = CInt(Right(number, 2))
    number = CStr(tempint)
    If tempint < 20 Then
        TenthPos = sUnits(tempint)
    ElseIf tempint > 19 Then
        If CInt(Right(number, 1)) <> 0 Then sp = " " Else sp = ""
        TenthPos = sTens(CInt(Left(number, 1))) & sp & sUnits(CInt(Right(number, 1)))
    End If
End Function
Private Function hundreds(number As String) As String
Dim tempint As Integer
tempint = CInt(Right(number, 3))
number = CStr(tempint)
    If Len(number) = 3 Then
        hundreds = sUnits(CInt(Left(number, 1))) & " " & strIdent(1) & " " & TenthPos(Right(number, 2))
    ElseIf Len(number) < 3 Then
        hundreds = TenthPos(number)
    End If
End Function
Private Function thousands(number As String) As String
Dim tempint As Double
Dim tempstr As String
tempstr = ""
tempint = CDbl(number)
number = tempint
    If Len(number) = 5 Then
        tempstr = Left(number, 2)
        thousands = TenthPos(tempstr)
        If CInt(tempstr) > 0 Then thousands = thousands & " " & strIdent(2)
        thousands = thousands & " " & hundreds(Right(number, 3))
    ElseIf Len(number) = 4 Then
        tempstr = Left(number, 1)
        thousands = TenthPos(tempstr)
        If CInt(tempstr) > 0 Then thousands = thousands & " " & strIdent(2)
        thousands = thousands & " " & hundreds(Right(number, 3))
    ElseIf Len(number) < 4 Then
        thousands = hundreds(number)
    End If
End Function
Private Function lacs(number As String) As String
Dim tempint As Double
Dim tempstr As String
tempstr = ""
tempint = CDbl(number)
number = tempint
    If Len(number) = 7 Then
        tempstr = Left(number, 2)
        lacs = TenthPos(tempstr)
        If CInt(tempstr) > 0 Then lacs = lacs & " " & strIdent(3)
        lacs = lacs & " " & thousands(Right(number, 5))
    ElseIf Len(number) = 6 Then
        tempstr = Left(number, 1)
        lacs = TenthPos(tempstr)
        If CInt(tempstr) > 0 Then lacs = lacs & " " & strIdent(3)
        lacs = lacs & " " & thousands(Right(number, 5))
    ElseIf Len(number) < 6 Then
        lacs = thousands(number)
    End If
End Function
Private Function Crores(number As String) As String
Dim tempint As Double
Dim tempstr As String
tempstr = ""
tempint = CDbl(number)
number = tempint
    If Len(number) = 9 Then
        tempstr = Left(number, 2)
        Crores = TenthPos(tempstr)
        If CInt(tempstr) > 0 Then Crores = Crores & " " & strIdent(4)
        Crores = Crores & " " & lacs(Right(number, 7))
    ElseIf Len(number) = 8 Then
        tempstr = Left(number, 1)
        Crores = TenthPos(tempstr)
        If CInt(tempstr) > 0 Then Crores = Crores & " " & strIdent(4)
        Crores = Crores & " " & lacs(Right(number, 7))
    ElseIf Len(number) < 8 Then
        Crores = lacs(number)
    End If
End Function

Private Sub Command1_Click()
'\ Calling function to convert in words
If Val(Text1) >= 0 Then
    If Text1.Text <> "" Then
        MsgBox sNumToWords(Text1.Text)
    End If
Else
    MsgBox "Please enter a valid number less than 10 Crores"
End If
End Sub

Private Sub Text1_KeyPress(KeyAscii As Integer)
'\ Using this code I am allowing to enter only number,Decimal are not allowed
    If (KeyAscii < 48 And KeyAscii <> 8) Or KeyAscii > 57 Then
        KeyAscii = 0
    End If
End Sub

@Sir there an error in my work the error says "The Expression On Click you entered as the event property setting produced the follwing error: Invalid Outside procedure.

On which line did the error occur?

Ok, that is because you are using ms access as your code editor and not vb6. The code above is for use in vb6. I'm not sure how you would call the function in access. You can try the following function and see if it works in access...

Public Function NumberToWords(OrigNum As Double) As String
    'This function converts numbers to words. For example 101 -> One hundred and one
    'It uses standard English notation and will only accept positive long numbers
    Dim billionpart As Long
    Dim millionpart As Long
    Dim decimalpart As Double
    Dim tmpstr As String
    Dim intpart As Long
    tmpstr = Format$(OrigNum, "0.00")
    tmpstr = Right(tmpstr, Len(tmpstr) - InStr(1, tmpstr, "."))
    decimalpart = CLng(tmpstr)
    intpart = CLng(OrigNum - CDbl("0." & tmpstr))
    'Now int part is correct and decimal
    billionpart = Int(intpart / 1000000000)
    millionpart = intpart Mod 1000000000
    NumberToWords = HundredsToWords(billionpart) & IIf(billionpart <> 0, " billion", "")
    If millionpart > 99 Then
        NumberToWords = NumberToWords & IIf(millionpart <> 0 And billionpart <> 0, " ", "") & millionstowords(millionpart)
    Else
        NumberToWords = NumberToWords & IIf(millionpart <> 0 And billionpart <> 0, " and ", "") & millionstowords(millionpart)
    End If
    'Now do decimal part bit
    NumberToWords = NumberToWords & " And " & CStr(decimalpart) & "/" & "100"
End Function

Thank you mrAndreret :D

It was a pleasure. Happy coding.:)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.