Partager via


Walkthrough: Creating Reports with Visual FoxPro

This walkthrough shows how to create Visual FoxPro reports and discusses issues to consider beyond direct data access, issues related to the needs for reports, and issues related to planning and creating online and offline reports. You can use the Report wizard to quickly get a report of any single or related tables. If you want to customize your report, or add non-data elements, use the Report designer.

Contents

Introduction

What is Data?

Planning a Report

Creating a Single-table Report

Creating a Multi-table Report

Introduction

Although Microsoft Visual FoxPro data can be reported to the screen simply as a Browse window or a form, properly designed reports lend clarity, readability, and distributability to data that turns it from developer data to user information. Reports are great for display and distribution of the information available from data, because they are easily repeated, duplicated, and shared.

It is easy to build simple reports in Visual FoxPro. Indeed, the very tools you use to create tables provide ways to immediately see all the data available (for more information, see Walkthrough: Creating Tables with Visual FoxPro.

The Report designer or Report wizard are helpful especially if you want to create a report that displays in one view the relationships or the combined information between two or more tables. This topic uses the Report wizard to create two reports — a simple single-table report and a multiple-table report.

What is Data?

Visual FoxPro deals with data in tables. Data is discrete and can be found in an ever-expanding range of types. Visual FoxPro can access most of it, but access is often not enough. To use data effectively, you must turn it into information — you must report it. This is not always a magical process. Usability can be as simple as a little pre-planning. Effectiveness can take more effort. Typically, you spend more time using or designing reports then you spend creating tables.

Planning a Report

With reports, not only is planning important, but it is recurrent and, sometimes, recursive. Use a report when you want to get the same information repeatedly or when you want to encourage specific activity or aid particular processes.

An effective report is one that makes it possible for you to use information quickly, whether you use all the available data or not. The way to start report planning is to think about how that report will be used.

Often, information is more inspiring or thought provoking when it is formatted properly or organized carefully. For example, if you are preparing for a staff meeting, it might be helpful to create reports in which data is organized by staff responsibilities, as well as by time, geographic, or financial relationships. If it is your address book or contacts list, you might find it helpful to create one or more reports organized by zip code (state), or birth month, phone area code, or hobby.

When you gather information for records in an address book or contact list, you enter all the information about one individual; however, you might create each record at different times and places. Even if you create several records at one time, you still complete one record before you begin the next. Several records can have identical values, but without a report it can be difficult to know which ones or how many. It can be important and useful to have a way to see these and other relationships.

Online or Printed Reports

If you are going to display data online or in print, there are some issues you should consider:

  • **Scrolling   **If your information cannot be displayed in one screen page, then consider separate pages rather than a long scroll. In addition, consider a different visual organization of the data, such as labels, positioning, or the use of white space.
  • **Labels   **This influences both the eye (where users look for information) and the mind (what users expect to find).
  • Spacing and positioning   This can be as important as font size for focus and clarity.
  • White space   Most people do not enjoy reading large blocks of text online, so good use of white space can make your report more effective.
  • **Location   **This is as important in screen real estate as it is in land. The top, the center, the right, and then the left are most often the important screen areas.

If your report can access background information or relevant data directly rather than always display it, then the basic report is more effective (and you have more room for the most important information).

Creating a Single-table Report

If you only want to look at what you have, use the Browse window on any table.

To access the Browse window

  1. From the File menu, open the table.
  2. On the View menu, select Browse.

If you want to print or mail a report quickly, you can export the file as text, and then print or mail the new text file. The Export dialog box options make it possible for you to select portions of the table to convert to text. (Remember to check the paper orientation for wide tables.)

To report raw data from a table as text quickly

  1. On the File menu, click Open.

  2. In the Open dialog box, select Table in the Files of type drop-down list, locate the table you want, and click OK.

  3. On the View menu, click Browse.

    This displays the selected table. At this time, in the IDE, you can change the column widths or locations, or you can make lasting changes from the Table menu.

  4. On the File menu, click Export.

  5. In the Export dialog box, select Delimited Text from the Type drop-down list.

    If you were going to use the table in another application, such as Microsoft Excel, you could select the application from the list, and the table would be converted to the proper format and saved with the correct extension.

  6. Click To, and enter the file name of your new file, including the extension.

  7. In the Field separator drop-down list, select the delimiter you want in the new file, and then click OK.

Visual FoxPro Report Wizard

If you find you must have formal presentations, summaries, or calculations, or you must use the same information repeatedly, then use the Report wizard or the Report designer to generate reusable, updateable, modifiable reports.

The Report wizard prompts you with choices that make it possible for you to create a well-formatted report of one or more tables quickly. In many circumstances, these quick reports work very well.

To use the Report wizard

  1. On the File menu, click New.
  2. In the New dialog box, select Report.
  3. Click the Wizard button, and select the type of report you want to create.

In the wizard, you can preview a report before you save it. In addition, you can save it and then open it in the Report designer to use the Visual FoxPro IDE to add to your report design.

To experiment with the Report wizard, use the Labels.dbf table, which ships with Visual FoxPro. Use the following procedures to work with simple, one-table reports

To create a simple Report using the Labels.dbf table

  1. On the File menu, click New.

  2. In the New dialog box, select Report and then click the Wizard button.

  3. In the Wizard selection dialog box, double-click Report Wizard.

  4. In the Report wizard, select Free Tables in the Databases and tables drop-down list box, and select or locate the Labels table.

  5. Select any or all of the available fields, and continue the wizard, making the appropriate selections up to Step 5.

  6. In Step 5, choose a report style and orientation.

    In addition, you can click Summary options to access five functions — Sum, Avg, Count, Min, and Max — for fields displayed in your report. The selected functions are added automatically with appropriate labeling and positioning.

  7. In Step 6, click the Preview button to see the report your selections would create if you click Finish. You can print the preview without creating a report file.

    The Report wizard creates labels and selects fonts, font sizes, indentation, and other styling and formatting characteristics to help turn the table data into report information. Often, creating reports with the wizard is as fast as creating the ad hoc Browse window copy, and the results often look much nicer.

  8. Click Finish.

For more information, see Report Wizard and One-To-Many Report Wizard.

Visual FoxPro Report Designer

You can use the Report designer to create simple reports, called Quick Reports. In addition, the Report designer is a powerful tool for creating formatted reports. The main difference between using the Report wizard and using the Report designer is that the designer provides greater control of the positioning of data and other content.

To create Quick Reports with the Report designer

  1. On the File menu, click New.

  2. In the New dialog box, select Report, and click the New File button.

  3. On the Report menu, click Quick Report.

    If you have a table open, Visual FoxPro will use it as the source for your report. If no table is open, Visual FoxPro displays the Open dialog box, so you can choose a table.

The Report designer populates the Detail and Page Footer bands. Then, the details are up to you. You have more control than with the Report wizard; you also have more responsibility. If you want to generate information, such as sums, averages, or counts, use Data Grouping on the Report menu. Your specification for a data group can be as simple as the name of the index field selected from the Expression builder.

To create a report containing a calculation

To perform this operation, open an ordered table (indexed on cust_id), create a Quick Report as basis, specify data grouping, and then specify how and where in the report the calculation is displayed. The final steps are to preview, print, and save the report.

  1. From the File menu, open the ..\samples\DATA\orders.dbf table.

  2. On the Window menu, click Data Session, and click Properties for the orders table.

  3. In the Work Area Properties dialog box, choose Orders.cust_id as the Index order, click OK, and then close the Data Session dialog box.

  4. On the File menu, click New. In the New dialog box, select Report, and then click New File.

  5. On the Report menu, click Quick Report, and then select the vertical layout.

    **Note   **For this example, you do not need all the fields, so you might want to remove the labels and fields following order_amt, as well as the labels and fields between emp_id and order_date. Move the fields together, and resize the report bands.

  6. On the Report menu, click Data Grouping.

  7. In the Data Grouping dialog box, click the ellipsis (...) button under Group Expressions. In the Fields list in the Expression builder, double-click Orders.cust_id. Click OK in both dialog boxes.

  8. In the Report Designer, place a copy of the order_amt field in the Group Footer band for group totals, and place another copy in the Page Footer for the entire table.

  9. Copy, paste, and then drag the copied fields from the Detail band.

  10. For each copy of order_amt, double-click the field to open the Report Expression dialog box.

  11. Click the Calculations button.

  12. Select the Sum function from the Option Group.

    The Reset drop-down list box should display the default selection, which is the Order.cust_id field. This means that the Sum function resets to zero each time the value of the cust_id field changes in the pass through the table. This is fine for the group sub totals, but you must change this selection for the order_amt field in the Page Footer band.

  13. After you select Sum in the Calculate Field dialog box of the order_amt field you placed in the Page Footer band, change the Reset value to End of Report.

  14. After setting the Calculation field of each order_amt, close the dialog boxes by clicking OK.

Use the print preview to see what you have created and to make sure that you are getting the information you expected. Save the report if you are satisfied with it. The last page displays the total for all orders — information not specifically included in the table.

While the Report designer is open, you have access to tools and other designers, so you can review the current table or modify the data environment by selecting other tables or indexes. In addition, you can add functionality to the report through the Report Controls toolbar and the Report menu.

For more information on the Report designer, see Report Designer and Report Designer Band Dialog Boxes.

Creating a Multi-table Report

In addition to single-table data, Visual FoxPro reports can display data from several related tables and can perform calculations and produce summaries.

This procedure uses two tables, customer and orders, from the testdata.dbc database in the Samples folder.

To create a report using the Testdata database that ships with Visual FoxPro

  1. On the File menu, click New.

  2. In the New dialog box, select Report, and click the Wizard button.

  3. In the Wizard selection dialog box, double-click One-To-Many Report Wizard.

  4. In the One-To-Many Report wizard, select the TESTDATA database in the Databases and Tables drop-down list, and select the Customer table.

  5. In Step 1 of the wizard, highlight the Customer table, and select the cust_id and Customer fields from the Available Fields drop-down list.

  6. In Step 2, highlight the Orders table, and select the cust_id, to_city, and order_amt fields from the Available Fields drop-down list.

    In Step 3, you see the way the two tables are related — customer.cust_id to orders.cust_id; in other words, the tables are indexed on matching fields.

    If you want a different relationship, you could choose from the drop-down lists for the parent and child tables. If you change the specified relationship, you would also have to make sure you had chosen the correct fields in steps 1 and 2.

  7. In Step 5, choose a report style and orientation.

    In addition, you can click Summary options to access five functions — SUM, AVG, COUNT, MIN, and MAX — for fields displayed in your report. The selected functions are added automatically with appropriate labeling and positioning.

  8. In Step 6, click Preview to see the report your selections would create if you click Finish. You can print the preview without creating a report file.

  9. Click Finish.

You can create the same report in the Report designer, but to specify the database and the tables, you must establish the data environment of the report. You then use that data environment in a Quick Report or through drag-and-drop in the IDE to create your report.

For samples of various kinds of report you can create, see the Solutions Samples.

For more information on using reports, see Using Expressions and Functions in Field Control in a Report.

See Also

Report Wizard | One-To-Many Report Wizard | Report Designer | Report Designer Band Dialog Boxes | Solutions Samples | Using Expressions and Functions in Field Control in a Report | Walkthroughs