Convert a number to words
Here is a function that converts a given numeric value to words in dollars or other currency. You can use this for your check writing application where you need to put the dollar amount in words in your checks.
For example:
? InWords(1234567890123.45)
should return:
One Trillion Two Hundred Thirty Four Billion Five Hundred Sixty Seven Million Eight Hundred Ninety Thousand One Hundred Twenty Three And 45/100 Dollars Only
Here’s the code:
Public Function InWords(varAmount As Variant) As String
Dim strInWords As String
Dim decAmount As Variant
Dim intHundreds As Integer
Dim intDecimal As Integer
Dim strThou(5) As String
Dim ctr As Byte
' Value should be less than 10 Trillion
If varAmount > 9999999999999.99@ Then
InWords = "*** Value too large to convert ***"
Exit Function
Else
decAmount = CDec(varAmount)
End If
strThou(1) = ""
strThou(2) = " Thousand "
strThou(3) = " Million "
strThou(4) = " Billion "
strThou(5) = " Trillion "
intDecimal = CInt((decAmount - Int(decAmount)) * 100)
decAmount = Int(decAmount)
For ctr = 1 To 5
intHundreds = ((decAmount / 1000) - Int(decAmount / 1000)) * 1000
If intHundreds > 0 Then
strInWords = Hundreds(intHundreds) & strThou(ctr) & strInWords
End If
If decAmount >= 1 Then
decAmount = Int(decAmount / 1000)
Else
Exit For
End If
Next ctr
InWords = strInWords & " And " & intDecimal & "/100 Dollars Only"
End Function
Public Function Hundreds(intAmount As Integer) As String
Static varOnes As Variant
Static varTens As Variant
Dim strWords As String
Dim bOnes As Byte
Dim bTens As Byte
Dim bTeens As Byte
Dim bHundreds As Byte
If intAmount > 999 Then
Hundreds = "***Parameter should be less than 999***"
Exit Function
End If
If IsEmpty(varOnes) Then
varOnes = Array("", "One", "Two", "Three", "Four", "Five", _
"Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", _
"Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", _
"Eighteen", "Nineteen")
End If
If IsEmpty(varTens) Then
varTens = Array("", "Ten", "Twenty", "Thirty", "Forty", _
"Fifty", "Sixty", "Seventy", "Eighty", "Ninety")
End If
bHundreds = Int(intAmount / 100)
If bHundreds > 0 Then
strWords = varOnes(bHundreds) & " Hundred "
End If
bTeens = ((intAmount / 100) - Int(intAmount / 100)) * 100
If bTeens < 20 Then
strWords = strWords & varOnes(bTeens)
Else
bTens = Int(intAmount / 10) - (Int(intAmount / 100) * 10)
If bTens > 0 Then
strWords = strWords & varTens(bTens) & " "
End If
bOnes = intAmount - (Int(intAmount / 10) * 10)
If bOnes > 0 Then
strWords = strWords & varOnes(bOnes)
End If
End If
Hundreds = strWords
End Function
Read More...
Wednesday, September 23, 2009 | 0 Comments
Allow User to Add Values in Combo Box
It is often good practice to provide users with a list of values that they can assign to a field when entering data into a Microsoft Access database form. With the use of pre-filled lists like a combo box, you can prevent the user from making incorrect data entry, and minimize the chance of spelling mistakes or incorrect choice of values. On some occasions, however, you may wish to allow the user to add new values to the underlying table. I do this for a field like City as part of a contact address where I would give the user the ability to define a new City that is not on the original list. This case would call for you to make use of the ComboBox’s NotInList event. To make it work, make sure that the following properties are set: Here’s the sample code for the NotInList event:
Private Sub cbxSYAddressCityID_NotInList( _
NewData As String, Response As Integer)
Dim strMsg As String
Dim strSQL As String
' Exit if the combo box was cleared
' Delete this line if the field is required
If Len(NewData) = 0 Then Exit Sub
' Confirm that the user wants to add the new city
strMsg = """" & NewData & """" & " is not in the list." _
& vbCr & vbCr & "Do you want to add it?"
If MsgBox(strMsg, vbQuestion + vbYesNo) = vbNo Then
' If the user chose not to add the city, set the
' Response argument to suppress an error message
' and undo changes.
Response = acDataErrContinue
' Display a customized message.
cbxSYAddressCityID.SetFocus
Else
' If the user chose to add the new city, insert
' it the SYCity table
strSQL = "INSERT INTO SYCity (SYCityName)" _
& " VALUES (" & """" & NewData & """" & ");"
' SetWarnings to False so the Dialog to confirm
' new record won't appear
DoCmd.SetWarnings False
DoCmd.RunSQL strSQL
' SetWarnings to True again
DoCmd.SetWarnings True
' Set Response argument to indicate that new data
' is being added.
Response = acDataErrAdded
End If
End Sub
Friday, September 04, 2009 | 0 Comments