Walkthrough: Creating Applications with Visual FoxPro 

 

McAlister Merchant
Microsoft Corporation

Created: March 2001
Revised: October 2001

Applies to:
     Microsoft® Visual FoxPro® 7.0

Summary: Visual FoxPro 7.0 can create simple programs or elaborate enterprise level applications, servers, and XML Web Services. Because of the unique design of Visual FoxPro, you can write a program, have Visual FoxPro create a program, or do any combination of both using code, menus, wizards, designers, builders, objects, and so on. (7 printed pages)

Contents

Introduction
Using Data
Creating a Program for a Form
Creating an Application

Introduction

Microsoft® Visual FoxPro® programs (.prg files) are created easily by using menu options or Visual FoxPro tools such as designers, wizards, and builders. Applications are Visual FoxPro programs that are compiled, after creation, into .app files in order to run in a Visual FoxPro environment, or into .exe or .dll files that can be called from anywhere in a Microsoft Windows® environment.

This paper walks you through the steps to create a simple program and then shows you how to use it as the basis for an application.

Using Data

Visual FoxPro programs and applications are based on data in tables or cursors. Data can exist already on your hard disk, a remote server, or a Web site, or you can create it in Visual FoxPro. Data can be in any of the formats that Visual FoxPro recognizes, including OLE objects.

For this sample application, the data you use will be copied from the Orders table, which is included with Visual FoxPro in the \Program Files\Microsoft Visual FoxPro 7\Samples\Data folder.

To move data from one table to another:

  1. From the File menu, choose Export.

  2. In Type drop-down list of the Export dialog box, select Visual FoxPro 3.0 (DBF), and in the To edit box, enter myApp as the file you want to create.

  3. Click the Ellipsis button (...) to open the Save As dialog box to save the file in your Visual FoxPro Projects folder (..\My Documents\Visual FoxPro Projects).

  4. In the From edit box, click the Ellipsis button (...), and locate the Orders.dbf table in \Program Files\Microsoft Visual FoxPro 7\Samples\Data folder.

  5. In the Export dialog box, click the Options button to open the Export Options dialog box.

  6. In the Export Options dialog box, click the Fields button, move cust_id, to_city, to_country, and order_amt from the Allfields list to the Selected fields list, and then click OK. Click OK twice more to close the Export Options dialog box and the Export dialog box.

  7. In the File menu, select Open.

  8. In the Filesof type drop-down list, specify Table, select the Exclusive check box, and browse to myApp.dbf.

  9. In the View menu, choose Table Designer.

  10. In the Table Designer, select the to_city row, click the Index drop-down list, and choose Ascending.

  11. Click OK, and click Yes to make the structure change permanent.

  12. From the Window menu, choose Data Session to order the table according to your new index.

    NoteYou have created a table that, because it is indexed, supports group calculations in the Report Designer.

  13. In the Data Session window, click Properties.

  14. In the Work Area Properties dialog box, select myApp:to_city in the Index Order drop-down list, and then click OK.

You have now exported selected fields from one table to another and set an index on the new table. Leave the Data Session window open and use this table immediately for the rest of this sample.

Creating a Program for a Form

You can create simple Visual FoxPro programs from the menus and the IDE. These programs either run in Visual FoxPro or access the Visual FoxPro Runtime.

To create the form:

  1. From the File menu, choose New.

  2. In the New dialog box, click Form, and then click New file.

    This opens the Form Designer, adds the Form menu and the Form Controls toolbar, and displays the Properties window for the form. If the toolbar is not visible, you can open it from the View menu.

  3. In the Properties window for the form, select Caption, and type myForm.

To add a label:

  1. In the Form Controls toolbar, click Label, and drag a rectangle on the form.
  2. In the Properties window for the label control, select Caption, and change it to My Data Form. Then, select FontSize, and type 20.
  3. If necessary, resize the label so the text fits.

To add a grid:

  1. In the Form Controls toolbar, click Grid, and then draw a rectangle on the form.
  2. Right-click the Grid control, and select Builder from the shortcut menu.
  3. In the Grid Builder, select the table, myApp.
  4. On the Grid Items tab, add all the fields in the Availablefields list to the Selected fields list, and then close the builder.

To add a button:

  1. In the Form Controls toolbar, click the Command Button icon and then draw a rectangle on the form below the grid.

  2. Double-click the newly created button, and, in the code editor, type the following code. Then close the code-editing window.

    THISFORM.RELEASE
    

    This code will close the form when you no longer need it.

  3. In the Properties window for the button control, select Caption and change it to Exit.

  4. From the File menu, save the form as myForm.

  5. Click the Do button (!) in the toolbar to preview your form.

To complete the program:

  1. From the File menu, choose New.

  2. In the New dialog box, click Program, and then click New file. This opens the Visual FoxPro editor.

  3. In the editor, type the following:

    DO FORM myForm.scx
    

  4. Save this program as myProgram. This creates a Visual FoxPro program (.prg) file that runs your form from the following command in the Command window:

    DO myProgram
    

Now you have taken a table and placed it (as a grid), a label, and a button on a form. You then created a program to run from the Command window or Program menu. In the next section, the application exchanges a data review form for the grid and adds it to the program before compiling it to a file.

Creating an Application

Running the program you created earlier requires Visual FoxPro to be installed on the computer. However, with Visual FoxPro, you can create an application that will run on a computer that does not have Visual FoxPro installed on it.

The earlier program has only one form and does not have much functionality. This section walks you through adding a data review form with some additional controls and then compiling the two forms—first into a Visual FoxPro application and then into a Windows application. A Visual FoxPro application (.app) file or a Visual FoxPro executable (.exe) file can each run on Windows platforms if Visual FoxPro is installed. A Visual FoxPro application compiled as an executable (.exe) file also can run on Windows platforms with only the Visual FoxPro Runtime installed.

In this section, you will create an application using the Application wizard, the Application builder, and the Project manager.

  • The Application wizard creates a project and provides the advanced application framework so you can add components you might have created already or create an application from start to finish, including splash screens and other enhancements.
  • The Application builder makes it possible for you to add tables, forms, and reports to an existing project.
  • The Project manager, the most fundamental application-building tool in Visual FoxPro, provides access to all the components you might want to include in an application and contains them for compilation.

For this walk through, first you use the Application wizard.

To create an application:

  1. From the Tools menu, choose Wizards, and select Application from the submenu.
  2. In the Application Wizard dialog box, enter MyApplication as the project name, use the Browse button to make sure the project is saved in your Visual FoxPro Projects folder (..\My Documents\Visual FoxPro Projects), and then click OK.

Visual FoxPro prepares to create a project file with the name you specified. When Visual FoxPro has finished creating the project, you see two windows: the Project manager and the Application builder. In the following procedures, you use the Application builder to add components and functionality to your application.

To specify splash screen and About dialog box information:

  1. In the General tab of the Application builder, click the Ellipsis button (...) by the Image box, and find and select the Visual FoxPro logo, fox.bmp, in your Visual FoxPro folder (..\Program Files\Microsoft Visual FoxPro 7). This image will be placed on your application splash screen.

    **Note   **If you close the Application builder and want to reopen it, from the Tools menu, choose Wizards, select All Wizards from the submenu, select Application Builder in the Wizard Selection dialog box, and then click OK.

  2. In the Credits tab of the Application builder, add the appropriate authoring and version information.

To specify the data source:

  • In the Data tab of the Application builder, click Select, and find and select myAPP (the table you used earlier in the simple program).

    **Note   **You can choose any available table. This tab also provides buttons, so you can create tables or a database easily.

To create a form and a report:

  • In the Data tab of the Application builder, you can select from lists of form and report templates that are part of the Advanced Application Framework (included in the Application builder). Accept the default values in the drop-down lists, and then click the Generate button.

    Visual FoxPro generates a form and a report from the table you specified and gives you access to them from the Forms and Reports tabs of the Application builder. These components become part of the project you are building, and the files are saved in the Forms folder or Reports folder in your Visual FoxPro Projects folder (..\My Documents\Visual FoxPro Projects).

To add controls to the form:

  1. In the Forms tab of the Application builder, click the Edit button for the MyApp form to open the Form Designer where you can make any additions or changes.

    As is, the form will display only the first record, so you must add at least one more control; in this procedure, you add two.

  2. In the Form Designer toolbox, click the Button icon, and drag a rectangle in the form. In the Caption property, type Next.

  3. Double click the Next button on the form, and, in the code editor, type the following code, and then close the code editing window:

    IF ! EOF()
    SKIP
    THISFORM.REFRESH
    ELSE
    GO TOP
    ENDIF
    

    This code steps through the table records and displays each record. You also could add another button, with appropriate code, to move back through the table.

  4. Copy the Next button, and place the copy at the far right of the form. In the Caption property, type Exit.

  5. Double click the Exit button, and, in the Click event edit window, replace the code in the window with the following code, and then close the window:

    THISFORM.RELEASE
    

    This code closes your form.

To add calculations to the report:

  1. In the Reports tab of the Application builder, click the Edit button for MyApp to open the Report Designer. Expand all the bands of the report to make it easier to perform the following operations.

    **Note   **This is where you can make any additions or changes, such as rearranging fields or adding controls.

  2. In the View menu of the Report Designer, choose Data Environment.

  3. Right click Data Environment, and select Properties from the shortcut menu.

    This will set the Properties window to the Data environment.

  4. In the Properties window, choose the cursor from the Properties Description drop-down list, and change the Order property to to_city to make grouping possible.

  5. In the Report menu of the Report Designer, choose Data Grouping.

  6. In the Data Grouping dialog box, click the Ellipsis button (...) under Group expressions, and in the Fields list in the Expression Builder, double-click to_city. Click OK in both dialog boxes.

  7. In the Report Designer, copy and paste the to_city element, and then drag the pasted element from the Detail band to the Group Footer band for group totals. Repeat to place another copy of the to_city element in the Page Footer for the entire table.

  8. For each copy of the to_city element, double-click the element to open the Report Expression dialog box.

  9. Click the Calculations button.

  10. Select the Count function from the radio buttons in the Calculation Field dialog box.

    The Reset drop-down list should display the default selection, which is the to_city field. This means the Count function will reset to zero each time the value of the to_city element changes in the pass through the table. This is correct for the Group Footer band, but you must change this selection for the to_city element in the Page Footer band.

  11. After you select Count in the Calculate Field dialog box of the to_city element you placed in the Page Footer band, change the Reset value to End of Report.

  12. After setting the Calculation field of each to_city element, click OK to close the dialog box.

  13. Click OK to close the Application builder.

**Note   **As well as the features you specified in the Application wizard, Visual FoxPro created components for your application. When you run your application, you will discover a customized File menu and a dialog box from which you can run your form and your report.

Now you can use the Project manager to review the components you have created. You will find that the Application wizard created many components, which you modified in the Application builder and the Form and Report designers. You can examine and modify these components in various Visual FoxPro tools, such as the designers or the Class Browser and Object Browser.

At this point, however, the most important use of the Project manager is to complete the compilation of your files into an application.

To complete the application:

  1. In the Project Manager, click the Build button.

  2. Of the options listed in the Build Options dialog box, select Application (app) to create an application to be run only in Visual FoxPro.

    **Note   **If you select Win 32 executable/COM server (exe), you create an application that can be run on a computer with only the Visual FoxPro Runtime installed.

  3. Click OK.

When the build process is complete, you have an application you can run in Visual FoxPro by double-clicking the .app file. When the program opens, it displays the splash screen and then the Quick Start dialog box from which you can view your form and your report. The application also provides a Quick Start menu item.

For more information on creating programs and applications, see "Application Wizard" and "Application Builder" in the Visual FoxPro Help.

For more information on the Project manager and for details on application build options, see the "Project Manager Window," "Application Development with Project Manager," and "Build Options Dialog Box" topics in the Visual FoxPro Help.

© Microsoft Corporation. All rights reserved.