Passing a Variable to a Compiled Program

Passing a Variable to a Compiled Program

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?

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.”

Share the Post:
XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes

ransomware cyber attack

Why Is Ransomware Such a Major Threat?

One of the most significant cyber threats faced by modern organizations is a ransomware attack. Ransomware attacks have grown in both sophistication and frequency over the past few years, forcing

data dictionary

Tools You Need to Make a Data Dictionary

Data dictionaries are crucial for organizations of all sizes that deal with large amounts of data. they are centralized repositories of all the data in organizations, including metadata such as