次の方法で共有


How to: Create Forms with Local and Remote Data

You can create forms that can be easily switched between using local data and data that is stored remotely (for example, on a database server). This allows you to create a prototype application using local or test data, then switch to remote or live data without substantial changes to your forms.

For example, if your Visual FoxPro application is a front end for a large customer table stored on a database server, you can create a local .dbf file that contains a small but representative sampling of the data. You can then create, test, and debug your forms based on this small set of data. When you're ready to distribute your application, you can link your form to the large data set.

The key to being able to switch between local and remote data is to make sure that you use views instead of directly linking your form (and its controls) to a table. To access remote data, you must use a view in any event. Therefore, to facilitate switching between local and remote data, create a view for the local data as well. When you create the form, you can add both views to its data environment, then switch between them as needed.

To create a form that can switch between local and remote data

  1. Create two views of the data, one that points to the remote data, and another that points to the local data.

  2. Create a new form.

  3. Open the Data Environment Designer for the form, and then add both views.

  4. Right-click the Data Environment Designer, and then choose Properties.

  5. In the Properties Window (Visual FoxPro), set the Alias Property for both cursors to the same name.

  6. Set the data environment's OpenViews Property to either 1Local Only or 2  Remote Only, depending on which view you wanted to use when running the form.

    Note

    Because you are using the same alias for both views, do not choose 0Local and Remote (the default).

  7. On the form, add the controls you need and set their ControlSource properties to the appropriate fields in the view. Because both views have the same alias, the controls will respond automatically to whichever view is active when the form is run.

After the form is created, you can switch the views alias by changing the data environment's OpenViews property. You can do this in the Data Environment while using the Form Designer. Alternatively, you can write code and attach it to an event, which is useful if you want to switch views at run time. For example, you could put this code in the form's Activate event:

THISFORM.DataEnvironment.OpenViews = 2 && Use remote view

If you create a form that can be switched between local and remote data, you must also design your navigation code to accommodate both views, particularly if you are designing forms with one-to-many relationships. For example, if your form only accesses a local table or view, you might use code such as the following in a Next command button to move to the next record in a cursor:

SKIP 1
THISFORM.Refresh()

However, this code is inefficient when you're navigating in a remote view, because it assumes that the cursor contains all the data required by the form. As a rule, you want to minimize the amount of data that you download from the remote data source.

The solution is to use a parameterized view. For example, the definition for a view used to edit customer information could be:

SELECT * FROM CUSTOMERS WHERE ;
 CUSTOMERS.COMPANY_NAME = ?pCompanyName

When the form runs, it can prompt the user for a customer name using a dialog box or by allowing the user to enter a name in a text box. The code for a Display button would then be similar to the following:

pCompanyName = THISFORM.txtCompanyName.Value
REQUERY("customer")
THISFORM.Refresh()

For more information, see How to: Create Parameterized Views.

See Also

Tasks

How to: Set the Design Area for a Form

How to: Set Form Templates

Reference

Data Environment Designer

Other Resources

Working with Forms

Creating Forms