Condividi tramite


Running Code Manually

Generally, you can do anything in the Command window that you can do in a program. You can run code in the Command window that you might usually execute in a program file. However, certain advantages exist when running code from a program. For more information, see Creating Programs.

The following example describes steps for viewing customer information in a table using the Visual FoxPro interface. Take note that some of the steps are echoed in the Command window.

To locate a record in a table

  1. From the File menu, choose Open.
  2. From the Files of type box, choose Table.
  3. In the list of files, double-click the customer.dbf file to open the Customer table.
  4. From the View menu, choose Browse to browse the table.

You can scroll through the table and look in the Company field for the customer, "Ernst Handel." However, you can perform the same task in the Command window.

To find a single order in a table programmatically

  • Type the following commands in the Command window:

    USE Customer
    LOCATE FOR Company = "Ernst Handel"
    BROWSE
    

For more information about the Command Window, see Command Window and Operating the Command window.

After you locate the order for this company, you might want to increase the maximum order amount by three percent.

To increase the maximum order amount

  1. Press the TAB key to move to the field named max_ord_amt.
  2. Multiply the value in the field by 1.03 and type the new value in the field.

To increase the maximum order amount programmatically

  • Type the following Visual FoxPro command in the Command window:

    REPLACE max_ord_amt WITH max_ord_amt * 1.03
    

It might be relatively simple to change the maximum order amount for a single customer, either using the interface or by typing code in the Command window.

However, suppose you wanted to increase the maximum order amount of every customer by three percent. Performing this task using the interface might be laborious and prone to mistakes. If you provide the appropriate commands in a program file, Visual FoxPro can perform this task quickly and easily without error.

The following example shows a block of code can be used in a program to open the Customer.dbf table, uses SCAN and ENDSCAN to perform the enclosed operations for every record in the table, and increases the maximum order amount for all customers in the table by three percent:

USE customer
SCAN
REPLACE max_ord_amt WITH ;
  max_ord_amt * 1.03
ENDSCAN

The semicolon (;) indicates that the command continues on the next line. For more information, see SCAN ... ENDSCAN Command and REPLACE Command.

See Also

Working with Programs | Creating Programs