Microsoft KB Archive/95640

= ACC: How to Convert Currency or Numbers into English Words =

Article ID: 95640

Article Last Modified on 1/18/2007

-

APPLIES TO


 * Microsoft Access 1.0 Standard Edition
 * Microsoft Access 1.1 Standard Edition
 * Microsoft Access 2.0 Standard Edition
 * Microsoft Access 95 Standard Edition
 * Microsoft Access 97 Standard Edition

-



This article was previously published under Q95640



Moderate: Requires basic macro, coding, and interoperability skills.



SUMMARY
This article shows you how to create a sample, user-defined function named ConvertCurrencyToEnglish to convert a numeric value to an English word representation. For example, the function will return the following words for the number 1234.56:

One Thousand Two Hundred Thirty Four Dollars And Fifty Six Cents

This article assumes that you are familiar with Visual Basic for Applications and with creating Microsoft Access applications using the programming tools provided with Microsoft Access. For more information about Visual Basic for Applications, please refer to your version of the &quot;Building Applications with Microsoft Access&quot; manual.

NOTE: Visual Basic for Applications is called Access Basic in Microsoft Access versions 1.x and 2.0. For more information about Access Basic, please refer to the &quot;Introduction to Programming&quot; manual in Microsoft Access version 1.x or the &quot;Building Applications&quot; manual in Microsoft Access version 2.0

NOTE: This article explains a technique demonstrated in the sample files, RptSampl.exe (for Microsoft Access for Windows 95 version 7.0) and RptSmp97.exe (for Microsoft Access 97). For information about how to obtain these sample files, please see the following articles in the Microsoft Knowledge Base:

145777 ACC95: Microsoft Access Sample Reports Available in Download Center

175072 ACC97: Microsoft Access 97 Sample Reports Available in Download Center



MORE INFORMATION
To create the ConvertCurrencyToEnglish function, follow these steps:  Create a new module and type the following line in the Declarations section if the line is not already there:

Option Explicit

  Type the following four procedures: Function ConvertCurrencyToEnglish (ByVal MyNumber) Dim Temp Dim Dollars, Cents Dim DecimalPlace, Count

ReDim Place(9) As String Place(2) = &quot; Thousand &quot; Place(3) = &quot; Million &quot; Place(4) = &quot; Billion &quot; Place(5) = &quot; Trillion &quot;

' Convert MyNumber to a string, trimming extra spaces. MyNumber = Trim(Str(MyNumber))

' Find decimal place. DecimalPlace = InStr(MyNumber, &quot;.&quot;)

' If we find decimal place... If DecimalPlace > 0 Then ' Convert cents Temp = Left(Mid(MyNumber, DecimalPlace + 1) & &quot;00&quot;, 2) Cents = ConvertTens(Temp)

' Strip off cents from remainder to convert. MyNumber = Trim(Left(MyNumber, DecimalPlace - 1)) End If

Count = 1 Do While MyNumber <> &quot;&quot; ' Convert last 3 digits of MyNumber to English dollars. Temp = ConvertHundreds(Right(MyNumber, 3)) If Temp <> &quot;&quot; Then Dollars = Temp & Place(Count) & Dollars If Len(MyNumber) > 3 Then ' Remove last 3 converted digits from MyNumber. MyNumber = Left(MyNumber, Len(MyNumber) - 3) Else MyNumber = &quot;&quot; End If           Count = Count + 1 Loop

' Clean up dollars. Select Case Dollars Case &quot;&quot; Dollars = &quot;No Dollars&quot; Case &quot;One&quot; Dollars = &quot;One Dollar&quot; Case Else Dollars = Dollars & &quot; Dollars&quot; End Select

' Clean up cents. Select Case Cents Case &quot;&quot; Cents = &quot; And No Cents&quot; Case &quot;One&quot; Cents = &quot; And One Cent&quot; Case Else Cents = &quot; And &quot; & Cents & &quot; Cents&quot; End Select

ConvertCurrencyToEnglish = Dollars & Cents End Function

Private Function ConvertHundreds (ByVal MyNumber) Dim Result As String

' Exit if there is nothing to convert. If Val(MyNumber) = 0 Then Exit Function

' Append leading zeros to number. MyNumber = Right(&quot;000&quot; & MyNumber, 3)

' Do we have a hundreds place digit to convert? If Left(MyNumber, 1) <> &quot;0&quot; Then Result = ConvertDigit(Left(MyNumber, 1)) & &quot; Hundred &quot; End If

' Do we have a tens place digit to convert? If Mid(MyNumber, 2, 1) <> &quot;0&quot; Then Result = Result & ConvertTens(Mid(MyNumber, 2)) Else ' If not, then convert the ones place digit. Result = Result & ConvertDigit(Mid(MyNumber, 3)) End If

ConvertHundreds = Trim(Result) End Function

Private Function ConvertTens (ByVal MyTens) Dim Result As String

' Is value between 10 and 19? If Val(Left(MyTens, 1)) = 1 Then Select Case Val(MyTens) Case 10: Result = &quot;Ten&quot; Case 11: Result = &quot;Eleven&quot; Case 12: Result = &quot;Twelve&quot; Case 13: Result = &quot;Thirteen&quot; Case 14: Result = &quot;Fourteen&quot; Case 15: Result = &quot;Fifteen&quot; Case 16: Result = &quot;Sixteen&quot; Case 17: Result = &quot;Seventeen&quot; Case 18: Result = &quot;Eighteen&quot; Case 19: Result = &quot;Nineteen&quot; Case Else End Select Else ' .. otherwise it's between 20 and 99. Select Case Val(Left(MyTens, 1)) Case 2: Result = &quot;Twenty &quot; Case 3: Result = &quot;Thirty &quot; Case 4: Result = &quot;Forty &quot; Case 5: Result = &quot;Fifty &quot; Case 6: Result = &quot;Sixty &quot; Case 7: Result = &quot;Seventy &quot; Case 8: Result = &quot;Eighty &quot; Case 9: Result = &quot;Ninety &quot; Case Else End Select

' Convert ones place digit. Result = Result & ConvertDigit(Right(MyTens, 1)) End If

ConvertTens = Result End Function

Private Function ConvertDigit (ByVal MyDigit) Select Case Val(MyDigit) Case 1: ConvertDigit = &quot;One&quot; Case 2: ConvertDigit = &quot;Two&quot; Case 3: ConvertDigit = &quot;Three&quot; Case 4: ConvertDigit = &quot;Four&quot; Case 5: ConvertDigit = &quot;Five&quot; Case 6: ConvertDigit = &quot;Six&quot; Case 7: ConvertDigit = &quot;Seven&quot; Case 8: ConvertDigit = &quot;Eight&quot; Case 9: ConvertDigit = &quot;Nine&quot; Case Else: ConvertDigit = &quot;&quot; End Select End Function  To test this function, type the following line in the Debug window (or Immediate window in version 1.x and 2.0), and then press ENTER:

? ConvertCurrencyToEnglish(1234.56)



How to Use the ConvertCurrencyToEnglish Function on a Form
To demonstrate the use of the ConvertCurrencyToEnglish function on a form, follow these steps:  Open the sample database Northwind.mdb (or Nwind.mdb in versions 1.x and 2.0). Open the Orders form in Design view and add a text box to the form. Set the text box's ControlSource property as follows:

=ConvertCurrencyToEnglish([Total])

</li> Open the Orders form in Form view. Note that the text box displays an English word representation of the total order amount computed in the form's Total field.</li></ol>

Additional query words: how to check writing numbers words report printing checks change into turn into

Keywords: kbhowto kbprogramming KB95640

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.