VB.Net Converting Number to Words

by demtron on Monday, February 02, 2009 09:30 AM

I found some example code on the Microsoft site at http://support.microsoft.com/kb/213360 that shows how to convert a decimal number to words using VBA.  This type of code is specially useful for tranforming currency amounts into words for printing on a check.  I needed to have this code converted to VB.Net, so here it is:


'Main Function
Function SpellNumber(ByVal MyNumber As String) As String
Dim Dollars As String = ""
Dim Cents As String = ""
Dim Temp As String = ""
Dim DecimalPlace, Count As Integer
Dim Place(9) As String
Place(2) = " Thousand "
Place(3) = " Million "
Place(4) = " Billion "
Place(5) = " Trillion "
' String representation of amount.
MyNumber = Trim(Str(MyNumber))
' Position of decimal place 0 if none.
DecimalPlace = InStr(MyNumber, ".")
' Convert cents and set MyNumber to dollar amount.
If DecimalPlace > 0 Then
Cents = GetTens(Left(Mid(MyNumber, DecimalPlace + 1) & _
"00", 2))
MyNumber = Trim(Left(MyNumber, DecimalPlace - 1))
End If
Count = 1
Do While MyNumber <> ""
Temp = GetHundreds(Right(MyNumber, 3))
If Temp <> "" Then Dollars = Temp & Place(Count) & Dollars
If Len(MyNumber) > 3 Then
MyNumber = Left(MyNumber, Len(MyNumber) - 3)
Else
MyNumber = ""
End If
Count = Count + 1
Loop
Select Case Dollars
Case ""
Dollars = "No Dollars"
Case "One"
Dollars = "One Dollar"
Case Else
Dollars = Dollars & " Dollars"
End Select
Select Case Cents
Case ""
Cents = " and No Cents"
Case "One"
Cents = " and One Cent"
Case Else
Cents = " and " & Cents & " Cents"
End Select
SpellNumber = Dollars & Cents
End Function
' Converts a number from 100-999 into text 
Function GetHundreds(ByVal MyNumber As String) As String
Dim Result As String
If Val(MyNumber) = 0 Then : Return "" : Exit Function : End If
MyNumber = Right("000" & MyNumber, 3)
' Convert the hundreds place.
If Mid(MyNumber, 1, 1) <> "0" Then
Result = GetDigit(Mid(MyNumber, 1, 1)) & " Hundred "
End If
' Convert the tens and ones place.
If Mid(MyNumber, 2, 1) <> "0" Then
Result = Result & GetTens(Mid(MyNumber, 2))
Else
Result = Result & GetDigit(Mid(MyNumber, 3))
End If
GetHundreds = Result
End Function
' Converts a number from 10 to 99 into text. 
Function GetTens(ByVal TensText As String) As String
Dim Result As String
Result = ""           ' Null out the temporary function value.
If Val(Left(TensText, 1)) = 1 Then   ' If value between 10-19...
Select Case Val(TensText)
Case 10 : Result = "Ten"
Case 11 : Result = "Eleven"
Case 12 : Result = "Twelve"
Case 13 : Result = "Thirteen"
Case 14 : Result = "Fourteen"
Case 15 : Result = "Fifteen"
Case 16 : Result = "Sixteen"
Case 17 : Result = "Seventeen"
Case 18 : Result = "Eighteen"
Case 19 : Result = "Nineteen"
Case Else
End Select
Else                                 ' If value between 20-99...
Select Case Val(Left(TensText, 1))
Case 2 : Result = "Twenty "
Case 3 : Result = "Thirty "
Case 4 : Result = "Forty "
Case 5 : Result = "Fifty "
Case 6 : Result = "Sixty "
Case 7 : Result = "Seventy "
Case 8 : Result = "Eighty "
Case 9 : Result = "Ninety "
Case Else
End Select
Result = Result & GetDigit _
(Right(TensText, 1))  ' Retrieve ones place.
End If
GetTens = Result
End Function
' Converts a number from 1 to 9 into text. 
Function GetDigit(ByVal Digit As String) As String
Select Case Val(Digit)
Case 1 : GetDigit = "One"
Case 2 : GetDigit = "Two"
Case 3 : GetDigit = "Three"
Case 4 : GetDigit = "Four"
Case 5 : GetDigit = "Five"
Case 6 : GetDigit = "Six"
Case 7 : GetDigit = "Seven"
Case 8 : GetDigit = "Eight"
Case 9 : GetDigit = "Nine"
Case Else : GetDigit = ""
End Select
End Function


Export Modules to Text Files in Microsoft Access

by demtron on Sunday, October 12, 2008 09:12 PM

I was recently asked to review the code differences between two Access databases.  One was the then-current production version of the database.  The other was a version that was being modified with new business rules.  As a first pass, I decided to export all the form, report, and code modules from each database into separate text files in separate folders, then use a source code comparison tool to compare both sets of files for changes.

I may need to create some code that will compare other objects such as tables and queries, but for right now, comparing source code files has proved to be a good start for getting a handle on the changes.  Here's the code I used to export all the modules.

Option Compare Database
Option Explicit
Public Sub CreateModuleFiles()
'On Error GoTo Err_Handler
Dim accObj As AccessObject  'Each module/form/report.
Dim bWasOpen As Boolean     'Flag to leave form/report open if it was open.
Dim strDoc As String        'Name of each form/report
'Stand-alone modules.
For Each accObj In CurrentProject.AllModules
Call WriteOutModules(accObj.Name, True)
Next
'Modules behind forms.
For Each accObj In CurrentProject.AllForms
strDoc = accObj.Name
bWasOpen = accObj.IsLoaded
If Not bWasOpen Then
DoCmd.OpenForm strDoc, acDesign, WindowMode:=acHidden
End If
If Forms(strDoc).HasModule Then
Call WriteOutModules("Form_" & accObj.Name, False)
End If
If Not bWasOpen Then
DoCmd.Close acForm, strDoc, acSaveNo
End If
Next
'Modules behind reports.
For Each accObj In CurrentProject.AllReports
strDoc = accObj.Name
bWasOpen = accObj.IsLoaded
If Not bWasOpen Then
'In Access 2000, remove the ", WindowMode:=acHidden" from the next line.
DoCmd.OpenReport strDoc, acDesign, WindowMode:=acHidden
End If
If Reports(strDoc).HasModule Then
Call WriteOutModules("Report_" & accObj.Name, False)
End If
If Not bWasOpen Then
DoCmd.Close acReport, strDoc, acSaveNo
End If
Next
End Sub
Private Function WriteOutModules(strModule As String, bIsStandAlone As Boolean)
Dim strCode As String
Dim bWasOpen As Boolean     'Flag applies to standalone modules only.
Dim lngLineNo As Long
If bIsStandAlone Then
bWasOpen = CurrentProject.AllModules(strModule).IsLoaded
End If
If Not bWasOpen Then
DoCmd.OpenModule strModule
End If
For lngLineNo = 1 To Modules(strModule).CountOfLines
strCode = strCode & Modules(strModule).Lines(lngLineNo, 1) & vbCrLf
Next
strCode = strCode & vbCrLf & vbCrLf & vbCrLf
If Not bWasOpen Then
On Error Resume Next
DoCmd.Close acModule, strModule, acSaveNo
End If
Dim intFile As Integer
'*** Set to next free open number ***
intFile = FreeFile()
Open "C:\output\modules\" & strModule & ".txt"   
For Output As #intFile
Print #intFile, strCode
Close #intFile
Debug.Print strModule & " complete"
DoEvents
End Function

Listing Object Library Reference Information in MS Access

by demtron on Friday, October 10, 2008 03:17 PM

When writing documentation for a Microsoft Access application, it's often important to provide information for on the object library references, especially for those apps that depend heavily on 3rd party or custom components. It's easy to obtain a list of references used, but what about attributes like File Version, Description, and Company? We can get these by opening the file properties in Windows Explorer and viewing the Version tab. If there are more than just a few references, this can be time consuming, especially if various attributes need to be copied and pasted elsewhere.

TypeLib comes to the rescue! TypeLib is a system DLL that can read the metadata contained in a DLL. Simply set a reference to %windir%\tlbinf32.dll, then add and run the following code in a module:

Public Sub ListAllReferences()
    Dim ref As Reference
    Dim tl As TLI.TypeLibInfo
    For Each ref In Access.Reference
        Set tl = New TLI.TypeLibInfo
        tl.ContainingFile = ref.FullPath
        Debug.Print ref.Name & "," & tl.HelpString & "," & ref.BuiltIn & "," & ref.FullPath
    Next ref
End Sub

Enjoy!


Powered by BlogEngine.NET 1.5.1.18
Theme by Mads Kristensen · Adapted by Demtron

Bookmark and Share

Calendar

<<  June 2017  >>
MoTuWeThFrSaSu
2930311234
567891011
12131415161718
19202122232425
262728293012
3456789

View posts in large calendar
Log in

Milwaukee SEO Company

Milwaukee Access Programmer/Developer

Milwaukee Website Designer and Developer



Marketing / SEO

Blog Directory
blogarama - the blog directory
TopOfBlogs
Milwaukee area SEO, SEM, ASP.Net