Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Visual FoxPro 7.0 - Form Data Environment

Status
Not open for further replies.

FoxDave

IS-IT--Management
Feb 12, 2003
15
GB
Is there a way of setting the data environment for a form programmatically rather than using the data environment GUI. I want to use a view as the cursor source for a form but be able to specify the database location which holds the view. I cannot see a way to set these programatically since dataenvironment is not a property of a form or formset.
The reason for doing this is that I want my application to be able to run from any location without having to set that location in the data environment GUI.
 
Hi FoxDave,

First, you can access the data environment programmatically, just like any other object. For example:

thisform.dataenvironmnet.cursor1.database = "MyDBC"

However, you cannot subclass the data environment (at least, not in VFP 7.0 and earlier).

In the case of changing the source of the database, be aware that you must do that before the DE opens the tables (obvious, really). Try doing it in the Load event of the form, or the BeforeOpenTables event of the DE (not sure which is better; maybe someone else knows).

Final point: If the application cannot find the database that is stored in the DE's Database properties, it will look for it along the VFP search path. So altering the Database property might not be necessary after all, provided that the directory in the property is not present on the user's system. Mike Lewis
Edinburgh, Scotland
 
FoxDave,
As Mike hints at - 8.0 has what I think you want. The following is from the "What's New" section in the VFP 8.0 help file:
"Creating, Subclassing, and Specifying a DataEnvironment Class

You can now define and create a subclass of the DataEnvironment class. When you use the Visual FoxPro IDE or the CREATE CLASS command to create a new class, Visual FoxPro provides the DataEnvironment class in the Based On list in the New Class dialog box. You can also specify the DataEnvironment class as a base class for the cBaseClassName parameter in the CREATE CLASS and MODIFY CLASS commands.

On a form, you can save the DataEnvironment class as a visual class library (.vcx) by selecting the form, choosing Save As Class from the File menu, and selecting DataEnvironment. All DataEnvironment properties, methods, and events are available as appropriate in the Properties window after opening the Class Designer.

You can specify and load an external DataEnvironment class in a form (.scx) at design or run time. You can set the form's DEClass and DEClassLibrary properties to specify an external DataEnvironment class and the class library that contains it so that when the form instantiates, the specified DataEnvironment class loads automatically.

For more information, see DataEnvironment Object, Form Object, DEClass Property and DEClassLibrary Property."

Rick
 
For Mike Lewis

Mike

I tested your suggestion of using
THISFORM.dataenvironment.cursor1.database = "MyDBC"
in the load event of the form. Unfortunately this produces an error. Looking through the help files this is expected. Help indicates that you should use the Open and Close Tables Methods of the DataEnvironment. However, it gives no examples of code constructs that may be used in those methods. Any ideas? Is it simply that you are opening and closing the tables and views associated with the form with the OPEN DATABASE and USE commands and setting the relationships with SET RELATION.
Your suggestion about relying on the PATH setting was very useful and the problem that I had was solved by ensuring that the PATH was set correctly. The reason I have been pursuing this other method is that I build my applications using a set of test databases. When the application is put into production I wanted to be sure that it was addressing the production databases. This of course will only be a problem when the production databases are accessed from the machine containing the test databases (the developers machine).
Thanks again for your help
Dave
 
Dave,

My present application is similar to yours I have separate test and production databases. In fact, I have five production databases, which could be spread over anything from one to five directories, and I cannot determine the locations of those directories at run time.

My solution (after some trial and error) is as follows:

- In the main program, figure out the five paths and add them to the PATH setting.

- In each form, set to .F. the AutoCloseTables and AutoOpenTables of the data environment. It is that step that you cannot do generically in VFP 7.0 or below, but presumably you can in 8.0, as Rick suggests.

- Open the needed tables explicitly in the Load event of each form. You say that gave rise to an error in your case; that might have been because you did not set to .F. the above two properties, and the tables were therefore already open by the time the Load fires. Sorry if I misled you on that point.

Anyroad, the above steps seem to work reliably for me. I still have hard-coded paths in the Database properties of the various cursors in the DE. These are pointing to my development databases, but that doesn't seem to matter provided that the relevant directories are not present on the production machine.

Hope this helps.

Mike



Mike Lewis
Edinburgh, Scotland
 
You could also try something similar to what I suggested for the following post:

thread184-482927 hope that helps.

- Bryon
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top