Question:
I am calling an .FXP from a batch file, but would like to pass a variable. Am I able to do this? How would FoxPro recognize it?
Answer:
You can put a PARAMETERS statement at the top of the PRG file to trap parameters used when calling a program.
For example, let’s say that you have a program called GeneralLedger.prg for which you want to pass the name of the month to run the general ledger accounting program. At the top of GeneralLedger.prg, put the following:
PARAMETERS tcMonthNameIF PCOUNT()=0 *-- no parameter passed, alert user and end MESSAGEBOX("Error: Please pass the name of the month") RETURNELSE MESSAGEBOX("Running GL for "+tcMonthName)ENDIF
Next, compile GeneralLedger.prg into an FXP file and put the following into the batch file:
GeneralLedger.fxp January
When you run the batch file, you will see a message box that says, “Running GL for January.”
There is an important thing to be aware of. When you use a PARAMETERS statement to capture parameters passed from outside VFP, the parameters can only contain character information. For example, if the batch file were the following:
GeneralLedger.fxp 12345
the value of tcMonthName would be the string value “12345,” not the numeric value 12345. What this means is that if you need the parameters to be anything other than characters, you will need to convert the passed character information into the required type. For example, let’s extend the GeneralLedger program so that it can accept a month name and the year to run the GL:
PARAMETERS tcMonthName,tcYearDO CASE CASE PCOUNT()=0 *-- no parameter passed, alert user and end MESSAGEBOX("Error: Please pass the name of the month and the year") RETURN CASE PCOUNT()=1 *-- one parameter passed, alert user and end MESSAGEBOX("Error: Please pass the name of the month and the year") RETURN OTHERWISE *-- Convert tcYear to Numeric lnYear = VAL(tcYear) MESSAGEBOX("Running GL for "+tcMonthName+', '+STR(lcYear))ENDIF
Notice how the code translates the second parameter (tcYear) to a numeric using the VAL() function. If the batch file contained the following:
GeneralLedger.fxp January 2000
you would see a message box that says, “Running GL for January, 2000.”