Share via


How to: Set the Data Environment

Each form or set of forms includes a data environment. The data environment is an object that includes the tables or views that the form interacts with and the relationships between tables that the form expects. You can use the data environment to automate opening and closing tables and views when the form is run. In addition, you can use the data environment to set the ControlSource property for controls by populating the ControlSource property in the Properties window with all the fields in your data environment.

Adding Tables to the Data Environment

You can visually design the data environment and save it with the form. When you add tables or views to the Data Environment designer, you can see the fields and indexes that belong to the table or view.

To add tables to the data environment

  1. Open the form in the Form Designer.

  2. On the View menu, click Data Environment.

    The Data Environment designer opens for the form's data environment.

  3. On the DataEnvironment menu, click Add.

  4. In the Add Table or View dialog box, click the database you want in the Database list.

  5. In the Select area, click Tables or Views.

  6. In the Tables in database list, click the table or view you want to add to the data environment.

  7. Click Add.

    Tip

    If no database or project is open, click Other to browse for and select a table.

You can also drag a table or view from an open project or the Database Designer to the Data Environment designer.

The following list includes commonly set data environment properties:

Setting Indexes for the Data Environment

You can set the order your records appear in your report by setting an index for the data environment.

To set an index for the Data Environment Designer

  1. From the View menu, choose Data Environment.

  2. From the shortcut menu, choose Properties.

  3. In the Properties Window, choose Cursor1 in the Object box.

  4. Choose the Data tab and select the Order property.

  5. Enter an index name.

    - OR -

    Select an index from the list of available indexes.

When the Data Environment Designer is active, the Properties window displays objects and properties associated with the data environment. Each table or view in the data environment, each relationship between tables, and the data environment itself is a separate object in the Object box of the Properties window.

Removing Tables from the Data Environment Designer

When you remove a table from the data environment, any relationships that the table is involved in are also removed.

To remove a table or view from the Data Environment Designer

  1. In the Data Environment Designer, select the table or view.

  2. From the DataEnvironment menu, choose Remove.

Setting Relationships in the Data Environment

If you add tables to the Data Environment Designer that have persistent relationships set in a database, the relationships are automatically added in the data environment. If the tables don't have persistent relationships, you can still relate them in the Data Environment Designer.

To set relationships in the Data Environment Designer

  • Drag a field from the primary table onto the matching index tag in the related table.

You can also drag a field from the primary table to a field in the related table. If there is no index tag in the related table corresponding to the field in the primary table, you're prompted to create the index tag.

Editing Relationships in the Data Environment

When you set a relation in the Data Environment Designer, a line between the tables indicates the relationship.

To edit the properties of the relationship

  • In the Properties window, select the relationship from the Object box.

The properties of the relationship correspond to clauses and keywords in the SET RELATION and SET SKIP commands.

The RelationalExpr property is set by default to the name of the primary key field in the primary table. If the related table is indexed on an expression, you need to set the RelationalExpr property to this expression. For example, if the related table is indexed on UPPER(cust_id), you need to set RelationalExpr to UPPER(cust_id).

If the relation is not a one-to-many relationship, set the OneToMany property to false (.F.). This corresponds to using the SET RELATION command without issuing SET SKIP.

Setting the OneToMany property of a relation to true (.T.) corresponds to issuing the SET SKIP command. When you skip through the parent table, the record pointer remains on the same parent record until the record pointer moves through all related records in the child table.

Note

If you want a one-to-many relationship in the form, set the OneToMany property to true (.T.), even if a persistent one-to-many relationship has been established in a database.

See Also

Tasks

How to: Create a New Form

How to: Specify a Form Type

How to: Display a Child Form Inside a Top-Level Form

Concepts

Creation of Single- and Multiple-Document Interfaces

Designing Forms

Reference

Add Table or View Dialog Box

Properties Window (Visual FoxPro)

Other Resources

Manipulating Objects

Creating Forms