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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

passing table field to program

Status
Not open for further replies.

rrussell1029

Programmer
Mar 12, 2009
3
0
0
Hi, I'm kind of new to VFP and I have a question. The guy who worked my job before me left a program that my boss said would turn a given number into a text field format, like what you would see on a check, IE 100597.65 becomes "One Hundred Thousand Five Hundred Ninety Seven dollars and 65/100". The problem is, neither of us can seem to pass a numeric field to it without it giving an error of "invalid subscript reference". I'm not too good at troubleshooting code, so I was hoping you guys could give me a hand. If someone could tell me what exactly I have to do to get this to work, it would be awesome, thanks! I'll post the code below so you can go through it.

Program:

******************************************************************************************
**
** PROGRAM: MTOC
** PURPOSE: Convert Currency of Numeric value to English Words.
**
** ENHANCEMENTS:
** 1. Comment Logic
**
******************************************************************************************
LPARAMETERS tNumToConvert

* Variable(s) & Array(s)
LOCAL NumToConvert,Results,Temp
LOCAL cDollars,cCents
LOCAL nDecimalPlace,nCount

STORE "" TO NumToConvert,Results,cDollars,cCents
STORE 0 TO nDecimalPlace
STORE 1 TO nCount

LOCAL ARRAY aPlace[9]
STORE "" TO aPlace
aPlace[2] = " Thousand "
aPlace[3] = " Million "
aPlace[4] = " Billion "
aPlace[5] = " Trillion "
aPlace[6] = " Quadrillion "
aPlace[7] = " Quintillion "
aPlace[8] = " Sextillion "
aPlace[9] = " Septillion "

* Convert Input to String
DO CASE
CASE EMPTY(tNumToConvert)
RETURN "0"

CASE INLIST(TYPE("tNumToConvert"),"N","Y")
IF tNumToConvert > 9999999999999.99
RETURN "**,***,***,***,***.**"
ENDIF
NumToConvert = ALLT(STR(ABS(tNumToConvert),16,2))

OTHERWISE
NumToConvert = IIF(LEFT(ALLT(tNumToConvert),1)=="-",SUBSTR(ALLT(tNumToConvert),2),ALLT(tNumToConvert))
ENDCASE

* Convert Cents
nDecimalPlace = AT(".",NumToConvert)
IF nDecimalPlace>0 THEN
cCents = ConvertTens(LEFT(SUBSTR(NumToConvert,nDecimalPlace+1)+"00",2))
NumToConvert = ALLT(LEFT(NumToConvert,nDecimalPlace-1))
ENDIF
DO CASE
CASE cCents==""
cCents = " and no/100"
CASE cCents=="One"
cCents = " and 01/100"
OTHERWISE
cCents = " and " + cCents + "/100"
ENDCASE

* Convert Dollars
DO WHILE !EMPTY(NumToConvert)
Temp = ConvertHundreds(RIGHT(NumToConvert,3))
IF !EMPTY(Temp) THEN
cDollars = Temp+aPlace[nCount]+cDollars
ENDIF
IF LEN(NumToConvert)>3 Then
NumToConvert = LEFT(NumToConvert,LEN(NumToConvert)-3)
ELSE
NumToConvert = ""
ENDIF
nCount = nCount + 1
ENDDO
*!* DO CASE
*!* CASE cDollars==""
*!* cDollars = "No Dollars"
*!* CASE cDollars=="One"
*!* cDollars = "One Dollar"
*!* OTHERWISE
*!* cDollars = cDollars + " Dollars"
*!* ENDCASE

RETURN cDollars + cCents

FUNCTION ConvertHundreds
LPARAMETERS NumToConvert
LOCAL Result
Result = ""
IF VAL(NumToConvert)==0 THEN
RETURN Result
ENDIF
NumToConvert = PADL(NumToConvert,3,"0")
IF LEFT(NumToConvert,1)#"0" THEN
Result = ConvertDigit(LEFT(NumToConvert,1))+" Hundred "
ENDIF
IF SUBSTR(NumToConvert,2,1)#"0" THEN
Result = Result+ConvertTens(SUBSTR(NumToConvert,2))
ELSE
Result = Result+ConvertDigit(SUBSTR(NumToConvert,3))
ENDIF
ConvertHundreds = ALLTRIM(Result)
RETURN ConvertHundreds
ENDFUNC

FUNCTION ConvertTens
LPARAMETERS MyTens
LOCAL Result
Result = ""
IF VAL(LEFT(MyTens,1))==1 THEN
DO CASE
CASE VAL(MyTens)==10
Result = "Ten"
CASE VAL(MyTens)==11
Result = "Eleven"
CASE VAL(MyTens)==12
Result = "Twelve"
CASE VAL(MyTens)==13
Result = "Thirteen"
CASE VAL(MyTens)==14
Result = "Fourteen"
CASE VAL(MyTens)==15
Result = "Fifteen"
CASE VAL(MyTens)==16
Result = "Sixteen"
CASE VAL(MyTens)==17
Result = "Seventeen"
CASE VAL(MyTens)==18
Result = "Eighteen"
CASE VAL(MyTens)==19
Result = "Nineteen"
ENDCASE
ELSE
DO CASE
CASE VAL(LEFT(MyTens,1))==2
Result = "Twenty "
CASE VAL(LEFT(MyTens,1))==3
Result = "Thirty "
CASE VAL(LEFT(MyTens,1))==4
Result = "Forty "
CASE VAL(LEFT(MyTens,1))==5
Result = "Fifty "
CASE VAL(LEFT(MyTens,1))==6
Result = "Sixty "
CASE VAL(LEFT(MyTens,1))==7
Result = "Seventy "
CASE VAL(LEFT(MyTens,1))==8
Result = "Eighty "
CASE VAL(LEFT(MyTens,1))==9
Result = "Ninety "
ENDCASE
Result = Result+ConvertDigit(RIGHT(MyTens,1))
ENDIF
ConvertTens = Result
RETURN ConvertTens
ENDFUNC

FUNCTION ConvertDigit
LPARAMETERS MyDigit
DO CASE
CASE VAL(MyDigit)==1
ConvertDigit = "One"
CASE VAL(MyDigit)==2
ConvertDigit = "Two"
CASE VAL(MyDigit)==3
ConvertDigit = "Three"
CASE VAL(MyDigit)==4
ConvertDigit = "Four"
CASE VAL(MyDigit)==5
ConvertDigit = "Five"
CASE VAL(MyDigit)==6
ConvertDigit = "Six"
CASE VAL(MyDigit)==7
ConvertDigit = "Seven"
CASE VAL(MyDigit)==8
ConvertDigit = "Eight"
CASE VAL(MyDigit)==9
ConvertDigit = "Nine"
OTHERWISE
ConvertDigit = ""
ENDCASE
RETURN ConvertDigit
ENDFUNC
 
Could you tell us which line of code caused the error. If you are running the code in the VFP development environment, you can click Suspend when the error comes up, then view the line in question in the debugger. If you need more info about how to do that, just ask.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro tips, advice, training, consultancy
Custom software for your business
 
Actually, I was trying to pass it a data field from the command window. I was trying to do something like "Replace check with mtoc(loanamt) for recno()>0".
 
Well, that should work OK. assuming Loanamt is a numeric or currency field, and Check is a character field. If not, you would get an error - but probably not an invalid subscript. It's hard to know without more information.

By the way, I'm puzzed about your use of "recno() > 0". It seems odd -- although it's unlikely to be relevant to your main problem.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro tips, advice, training, consultancy
Custom software for your business
 
Isn't the error "invalid subscript reference" often to do with arrays?

What happens if you try a simple
Code:
? mtoc(99)

When running your replace statement, do any of the fields "check" get replaced?

Stewart

Hope that helps,

Stewart
PS If you want to get the best response to a question, please check out FAQ184-2483 first.
 
If I use the prg as provided mtoc(99) gives "Ninety Nine Thousand an no/100". That's because SET POINT is "," in Germany. So make sure you have SET POINT TO "."

Your error "invalid subscript reference" tells me, foxpro interprets your call to MTOC() as accessing an array name MTOC().

Before you can call a prg VFP must know it's in it's scope. You seem not to have SET PROCEDURE, foxpro does not know a function MTOC until you point it to that PRG.

Code:
SET PATH TO ...\prgs\ ADDITIVE
SET PROCEDURE TO mtoc.prg ADDITIVE

Now you're setup up to use that prg.

Bye, Olaf.
 
Yes, some of the fields get replaced if I ignore the error several times in a row, it finally gives me another error saying that aplace.prg does not exist. And when it does that, it replaces that record's check field with "and no/100" regardless of what that field's loanamt number was. I tried it at home with some sample data, and it worked fine for me as soon as I did that set procedure to mtoc.prg line, but it still won't work properly on the database I'm trying to use it on...could it just be bad data that I'm trying to pass to it?
 
That's how it sounds to me.

As Mike Lewis said, if you can run it in the development environment on the "real" database, then suspend execution when you get the error and have a look at the record it's working on and see if that gives you a clue.

Hope that helps,

Stewart
 
can you read me? A simple SET PROCEDURE TO is missing... your program calls are interpreted as array access.

Bye, Olaf.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top