Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need a few pointers

Status
Not open for further replies.

Anthony1312002

Programmer
Mar 4, 2004
146
US
I was directed to this script that converts numerical amounts into words using Visual Basic. I'm trying to make some changes to it so that I can use it in ASP/VBScript but with no success. The variable I'm trying to convert and display is CashC1. I'm unfamiliar with functions such as ParsChunk, Chunk and GoSub. I hope someone is willing to help. Thanks in advance.


'--------------------------------------
Function cWord2(num)
'first set up two arrays to convert numbers to words
Dim BigOnes(9)
Dim SmallOnes(19)

'and populate them
BigOnes(1) = "Ten"
BigOnes(2) = "Twenty"
BigOnes(3) = "Thirty"
BigOnes(4) = "Forty"
BigOnes(5) = "Fifty"
BigOnes(6) = "Sixty"
BigOnes(7) = "Seventy"
BigOnes(8) = "Eighty"
BigOnes(9) = "Ninety"

SmallOnes(1) = "One"
SmallOnes(2) = "Two"
SmallOnes(3) = "Three"
SmallOnes(4) = "Four"
SmallOnes(5) = "Five"
SmallOnes(6) = "Six"
SmallOnes(7) = "Seven"
SmallOnes(8) = "Eight"
SmallOnes(9) = "Nine"
SmallOnes(10) = "Ten"
SmallOnes(11) = "Eleven"
SmallOnes(12) = "Twelve"
SmallOnes(13) = "Thirteen"
SmallOnes(14) = "Fourteen"
SmallOnes(15) = "Fifteen"
SmallOnes(16) = "Sixteen"
SmallOnes(17) = "Seventeen"
SmallOnes(18) = "Eighteen"
SmallOnes(19) = "Nineteen"

'format the incoming number to guarantee six digits
'to the left of the decimal point and two to the right
'and then separate the dollars from the cents
CashC1 = FormatCurrency(CashC1, "000000.00")
Dollars = Left(CashC1, 6)
Cents = Right(CashC1, 2)

Words = ""

'separate the dollars into chunks
If Dollars = 0 Then
Words = "Zero"
Else

'first do the thousands
Chunk = Left(Dollars, 3)
If Chunk > 0 Then
GoSub
ParseChunk
Words = Words & " Thousand"
End If

'do the rest of the dollars
Chunk = Right(Dollars, 3)
If Chunk > 0 Then
GoSub
ParseChunk
End If
End If

'concatenate the cents and display
If Cents = 0 Then Cents = "No"
Words = Words & " and " & Cents & "/100"
Text2.Text = Words
End Function

ParseChunk:
digits = Mid(Chunk, 1, 1)
If digits > 0 Then
Words = Words & " " & SmallOnes(digits) & " Hundred"
End If
digits = Mid(Chunk, 2, 2)
If digits > 19 Then
leftdigit = Mid(Chunk, 2, 1)
rightdigit = Mid(Chunk, 3, 1)
Words = Words & " " & BigOnes(leftdigit)
If rightdigit > 0 Then
Words = Words & " " & SmallOnes(rightdigit)
End If
Else
If digits > 0 Then
Words = Words & " " & SmallOnes(digits)
End If
End If
Return
End Function
 
In the code that you posted...

Chunk is a variable name:

ParsChunk is a label...

GoSub tells the code to jump to another part of the code.

I think the problem you have is that GoSub ParsChunk should be on one line... that the code wants to jump down to the ParsChunk label and then when it hits Return it jumps back to the line after the GoSub.

Make sense?
 
Thanks for the quick response. I made the needed change but I'm getting a Expected Statement error at the last line 267 which is the End Function line. What's wrong?
 
You dont have an End If for that last IF/THEN before the exit function
 
Oh nevermind that was wrong, I didnt notice that it was just an IF statement all on one line so you dont need the End If
 
Ahh, it should be Exit function instead of END function when you do it in the middle like that

There should only be 1 END function statement per function
 
Sheco, thanks for the response. I changed all the end functions to exit functions and now there are no more error messages. Just one more question? How do I display the results? I know it should be simple but I'm missing it somehow.
 
Sorry, It might help to see everything I'm trying to do. Here is the code along with the recordset that I'm trying to convert.

Recordset
Do Until RSData1.EOF
Dim CashC1, Cash1Legal
CashC1 = CashC1 + RSData1("CashAmount1")
AccountOne = RSData1("AccountNumber")
Group1 = RSData1("Custgroups")
RSData1.MoveNext
Loop

Coversion Function
Function cWord2(num)
'first set up two arrays to convert numbers to words
Dim BigOnes(9)
Dim SmallOnes(19)

'and populate them
BigOnes(1) = "Ten"
BigOnes(2) = "Twenty"
BigOnes(3) = "Thirty"
BigOnes(4) = "Forty"
BigOnes(5) = "Fifty"
BigOnes(6) = "Sixty"
BigOnes(7) = "Seventy"
BigOnes(8) = "Eighty"
BigOnes(9) = "Ninety"

SmallOnes(1) = "One"
SmallOnes(2) = "Two"
SmallOnes(3) = "Three"
SmallOnes(4) = "Four"
SmallOnes(5) = "Five"
SmallOnes(6) = "Six"
SmallOnes(7) = "Seven"
SmallOnes(8) = "Eight"
SmallOnes(9) = "Nine"
SmallOnes(10) = "Ten"
SmallOnes(11) = "Eleven"
SmallOnes(12) = "Twelve"
SmallOnes(13) = "Thirteen"
SmallOnes(14) = "Fourteen"
SmallOnes(15) = "Fifteen"
SmallOnes(16) = "Sixteen"
SmallOnes(17) = "Seventeen"
SmallOnes(18) = "Eighteen"
SmallOnes(19) = "Nineteen"

'format the incoming number to guarantee six digits
'to the left of the decimal point and two to the right
'and then separate the dollars from the cents
CashC1 = FormatCurrency(CashC1, "000000.00")
Dollars = Left(CashC1, 6)
Cents = Right(CashC1, 2)

Words = ""

'check to make sure incoming number is not too large
If Dollars > 999999 Then
Cash1Legal = "Dollar amount is too large"
Exit Function
End If

'separate the dollars into chunks
If Dollars = 0 Then
Words = "Zero"
Else

'first do the thousands
Chunk = Left(Dollars, 3)
If Chunk > 0 Then
GoSub ParseChunk
Words = Words & " Thousand"
End If

'do the rest of the dollars
Chunk = Right(Dollars, 3)
If Chunk > 0 Then
GoSub ParseChunk
End If
End If

'concatenate the cents and display
If Cents = 0 Then Cents = "No"
Words = Words & " and " & Cents & "/100"
Cash1Legal = Words
Exit Function

ParseChunk:
digits = Mid(Chunk, 1, 1)
If digits > 0 Then
Words = Words & " " & SmallOnes(digits) & " Hundred"
End If
digits = Mid(Chunk, 2, 2)
If digits > 19 Then
leftdigit = Mid(Chunk, 2, 1)
rightdigit = Mid(Chunk, 3, 1)
Words = Words & " " & BigOnes(leftdigit)
If rightdigit > 0 Then
Words = Words & " " & SmallOnes(rightdigit)
End If
Else
If digits > 0 Then
Words = Words & " " & SmallOnes(digits)
End If
End If
Return
End Function
 
There are 2 small problems.

1. Your function value is not being assigned.
To assign a value to your function, set its value before you exit the function. So this:

If Dollars > 999999 Then
Cash1Legal = "Dollar amount is too large"
Exit Function
End If

should be:

If Dollars > 999999 Then
cWord2 = "Dollar amount is too large"
Exit Function
End If

and this:

'concatenate the cents and display
If Cents = 0 Then Cents = "No"
Words = Words & " and " & Cents & "/100"
Cash1Legal = Words
Exit Function

should be:

'concatenate the cents and display
If Cents = 0 Then Cents = "No"
Words = Words & " and " & Cents & "/100"
cWord2 = Words
Exit Function

2. You don't appear to be calling the function anywhere.

This:
Do Until RSData1.EOF
Dim CashC1, Cash1Legal
CashC1 = CashC1 + RSData1("CashAmount1")
AccountOne = RSData1("AccountNumber")
Group1 = RSData1("Custgroups")
RSData1.MoveNext
Loop

Should be:
Do Until RSData1.EOF
Dim CashC1, Cash1Legal
CashC1 = CashC1 + RSData1("CashAmount1")
AccountOne = RSData1("AccountNumber")
Group1 = RSData1("Custgroups")
RSData1.MoveNext
Loop
MsgBox cWord2(CashC1) ' calls the cWord2 function with CashC1 as argumemt

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

For tsunami relief donations

'If we're supposed to work in Hex, why have we only got A fingers?'

for steam enthusiasts
 
Thanks Johnwm, I made the changes but I'm getting a Permission denied error on the message box line. Is this a Java script error? How can I fix it?
 
That line was a hangover from the VB tryout, and just illustrates how you can call the function.

myNewString = cWord2(CashC1) ' converts the value CashC1 to string value

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

For tsunami relief donations

'If we're supposed to work in Hex, why have we only got A fingers?'

for steam enthusiasts
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top