HOWTO: Assign Database Location Generically in Data Env
ID: Q128996
|
The information in this article applies to:
-
Microsoft Visual FoxPro for Windows, versions 3.0, 5.0, 6.0
SUMMARY
The Data Environment contains information about all the tables, views, and
relationships that interact with a form. In the Form Designer, when a table
is added to the data environment, the database property of a cursor is
updated with the location and name of the DataBase container (.DBC), and
the path written in the DataBase property is set by default as an absolute
path.
If the database cannot be found when the form is opened, the error "Error
instantiating cursor object. Cannot find ..<database>" is displayed.
The sample code provided in this article modifies the path information
written in the Database property to fit your needs.
MORE INFORMATION
When a table is added to the Data Environment in the Form Designer, a
cursor object is created. If the table belongs to a DBC, the DataBase
property of the cursor is updated with the full path name to the DBC.
However, the absolute path to a DBC might need to be modified in a
distributed application. For example, multiple users can open databases
with the same name that are placed in different locations.
If you need to reference a different database than the one specified in the
Database property of the Cursor object, you can use the SET PATH command,
before the program calls the form, to set a path to the location of the
database. If the database specified in the DATABASE property of the Cursor
cannot be found, Visual FoxPro will search along the path.
Another alternative is to update the DATABASE property at runtime. The
following example adds flexibility to your code by updating the path
information in the Database property of the cursor object. This code can
update the location of different databases referenced in the Data
Environment. As this sample only references one directory, you will need to
modify it if you need to open databases in several directories.
To use this example:
- Create two variables that will be available to the form
- Data_Drive. This variable contains the drive letter and a colon.
Data_Drive = "C:"
- Data_Path. This variable contains the relative path with a backslash
as the last character.
Data_Path = "\VFP\SAMPLES\DATA\"
- Place the following code in the BeforeOpenTables Event of the
DataEnvironment. The WAIT WINDOW commands are for illustrating purposes,
and you can remove them.
IF !EMPTY(DATA_PATH) and !EMPTY(DATA_DRIVE)
* Make a list of all the cursors in the Data Environment
=AMEMBERS(A_Cursors,THISFORM.dataenvironment,1)
=ASORT(A_Cursors, 2)
nStartpos=ASUBSCRIPT(A_Cursors, ASCAN(A_Cursors, "Object"),1)
FOR I = nstartpos TO ALEN(A_cursors,1)
IF A_Cursors(i,2) = "Object"
cObjClass = "THISFORM.DATAENVIRONMENT."+a_cursors(i,1)+".class"
IF EVAL(cObjClass)="Cursor"
cObjName="THISFORM.DATAENVIRONMENT." ;
+ A_Cursors(i,1)+".DATABASE"
WAIT WINDOW cObjName
Data_Name=EVAL(cObjName)
WAIT WINDOW "This is Data_Name : "+ Data_Name
* Modify the path to the database
NewDataPath=ALLTRIM(data_drive)+ ALLTRIM(data_path) ;
+ ALLTRIM(SUBSTR(Data_Name, RAT("\",Data_Name)+1))
WAIT WINDOW newdatapath
* Evaluate the cursor object
oRef = EVAL( "THISFORM.DATAENVIRONMENT."+a_cursors(i,1) )
*Modify the Database property with the new path
oRef.Database = newdatapath
ENDIF
ELSE
EXIT
ENDIF
ENDFOR
ENDIF
Additional query words:
Keywords : kbnokeyword kbVFp300 kbVFp500 kbVFp600
Version :
Platform :
Issue type : kbhowto
|