Answered by:
working with two different set of data

Question
-
hi,
What is the best way to make an application switch between two different sets of data on the fly. the two database have the same structure but are located in two differnt folders.
Thanks a lot
Sunday, April 14, 2013 7:13 PM
Answers
-
Well, the code you used finally switches SET PATH, not the database, so it's for working with FREE tables, not a DBC.
Also SET DATABASE TO somedatabase.dbc only works, if you open multiple databases and it won't close tables and repoen them, so there is more to do anyway.
If you only need to switch data at application start it's simply reading the path of the DBC from some config file and OPEN DATABASE or SET PATH or both, but if your application is already started and has open several tables, you need
CLOSE TABLES ALL
CLOSE DATABASES ALLand then OPEN DATABASE (otherpath) or SET PATH (otherpath).
You also need to ensure the database you don't want to open is not within the search path, especially if you make use of free tables and the DE and depend on the automatic search and discovery of files along all paths you set with SET PATH (ADDITIVE).
Just SET DATABASE TO is a bit oversimplyfying things. If dbc and table files are named the same, SET DATABASE just switches the active dbc, you then have to have both databases open and if you have still tables of location1 open when you switch to location2, they remain open and tables and data would mix. So that's oversimplifying the process of switching to a seperate database.
Indeed you have to explain your situation in more detail, what "the two database have the same structure" means exactly. First question is: Do you really mean a DBC with database? Because in the old fox 2.x world this also was a term for a single free table and the term is loosely used also for a directory of free tables. If so, then CLOSE/OPEN DATABASE and SET DATABASE are of no use at all.
Bye, Olaf.
Olaf Doschke (Setmics)
- Proposed as answer by Ed Price - MSFTMicrosoft employee Monday, April 22, 2013 10:29 PM
- Marked as answer by Youen Zen Thursday, April 25, 2013 8:28 AM
Tuesday, April 16, 2013 12:21 PM -
Wow, that is a complicated solution for this problem.
SET DATABASE TO <database.Dbc>
After that, data will get pulled from the desired tables. This assumes that your tables are in a Dbc and not free tables.
Craig Berntson
MCSD, Visual C# MVP
INETA Community Speaker
www.craigberntson.com- Proposed as answer by Ed Price - MSFTMicrosoft employee Monday, April 22, 2013 10:29 PM
- Marked as answer by Youen Zen Thursday, April 25, 2013 8:28 AM
Monday, April 15, 2013 12:33 PM -
IF Native DBC OR FREE :
USE dbcname.tablename ALIAS alias1
RE. USE command
IF SQL TABLE :
CREATE SYNONYM for datasource in sp_
azizsallam
- Edited by abd el aziz sallam Saturday, April 20, 2013 9:10 PM
- Proposed as answer by Ed Price - MSFTMicrosoft employee Monday, April 22, 2013 10:29 PM
- Marked as answer by Youen Zen Thursday, April 25, 2013 8:28 AM
Saturday, April 20, 2013 9:08 PM
All replies
-
hi,
What is the best way to make an application switch between two different sets of data on the fly. the two database have the same structure but are located in two differnt folders.
Thanks a lot
I have a similar system where I have different databases in different folders. I have a form where you can select the different datapath as required and save the path in a "Free Table" locally called "WkStation". Each PC can have a different path to the data.
This is the code I used to save the path of Database.
LOCAL lcSys16, lcProgram, lcPath, lcOldDir,cDataPath
* Makes sure the Data Path is ended properly with the Folder Path
IF RIGHT(ALLTRIM(THISFORM.Text1.Value),1) = "/" .OR. RIGHT(ALLTRIM(THISFORM.Text1.Value),1) = "\"
ELSE
=MESSAGEBOX("Please set the Data Path ending with '\' or '/'" + CHR(13) + ;
"as required.", 0 , "Data Path Setting")
THISFORM.Text1.SetFocus()
RETURN 0
ENDIF
= TABLEUPDATE()
cDataPath = ALLTRIM(WkStation.cVariable)
*** CALCPATH
*** Automatically sets the path to include VFP Home directory
*** Get the name of program that called this one.
lcSys16 = SYS(16, 1)
*** Save current working directory
lcOldDir = (SYS(5)+CURDIR())
*** Make the directory from which it was run current
lcProgram = SUBSTR(lcSys16, AT(":", lcSys16) - 1)
CD LEFT(lcProgram, RAT("\", lcProgram))
IF INLIST( JUSTEXT( lcProgram ), "FXP", "SCX" )
*** If we are running a PRG/Form directly, then find the parent directory
CD ..
*** Set up path to include VFP Home plus the standard DEV directory tree
lcPath = (HOME()+';'+SYS(5)+CURDIR()+ ;
";PROGS DONE;DATA;FORMS;LIBS;PROGS;UTILS; MENUS; REPORTS")
ELSE
*** We are using an EXE/APP! Adjust path for DISTRIBUTION directory tree
lcPath = (HOME()+';'+SYS(5)+ CURDIR()+";"+ cDataPath)
ENDIF
*** Restore original directory
CD (lcOldDir)
*** Return the calculated path
SET PATH TO &lcPath
MESSAGEBOX("Data Path has been Saved", 0 + 64, "Set Data Path")
THISFORM.ReleaseI dont know if this is the best way or not, but it works well for me.
- Edited by July Derek Monday, April 15, 2013 7:05 AM
Monday, April 15, 2013 6:42 AM -
hi,
What is the best way to make an application switch between two different sets of data on the fly. the two database have the same structure but are located in two differnt folders.
Thanks a lot
I have a similar system where I have different databases in different folders. I have a form where you can select the different datapath as required and save the path in a "Free Table" locally called "WkStation". Each PC can have a different path to the data.
This is the code I used to save the path of Database.
LOCAL lcSys16, lcProgram, lcPath, lcOldDir,cDataPath
* Makes sure the Data Path is ended properly with the Folder Path
IF RIGHT(ALLTRIM(THISFORM.Text1.Value),1) = "/" .OR. RIGHT(ALLTRIM(THISFORM.Text1.Value),1) = "\"
ELSE
=MESSAGEBOX("Please set the Data Path ending with '\' or '/'" + CHR(13) + ;
"as required.", 0 , "Data Path Setting")
THISFORM.Text1.SetFocus()
RETURN 0
ENDIF
= TABLEUPDATE()
cDataPath = ALLTRIM(WkStation.cVariable)
*** CALCPATH
*** Automatically sets the path to include VFP Home directory
*** Get the name of program that called this one.
lcSys16 = SYS(16, 1)
*** Save current working directory
lcOldDir = (SYS(5)+CURDIR())
*** Make the directory from which it was run current
lcProgram = SUBSTR(lcSys16, AT(":", lcSys16) - 1)
CD LEFT(lcProgram, RAT("\", lcProgram))
IF INLIST( JUSTEXT( lcProgram ), "FXP", "SCX" )
*** If we are running a PRG/Form directly, then find the parent directory
CD ..
*** Set up path to include VFP Home plus the standard DEV directory tree
lcPath = (HOME()+';'+SYS(5)+CURDIR()+ ;
";PROGS DONE;DATA;FORMS;LIBS;PROGS;UTILS; MENUS; REPORTS")
ELSE
*** We are using an EXE/APP! Adjust path for DISTRIBUTION directory tree
lcPath = (HOME()+';'+SYS(5)+ CURDIR()+";"+ cDataPath)
ENDIF
*** Restore original directory
CD (lcOldDir)
*** Return the calculated path
SET PATH TO &lcPath
MESSAGEBOX("Data Path has been Saved", 0 + 64, "Set Data Path")
THISFORM.ReleaseI dont know if this is the best way or not, but it works well for me.
very smartMonday, April 15, 2013 7:56 AM -
I had copied this idea from the Book "1001 Things you wanted to know about Visual FoxPro" :)
- Edited by July Derek Monday, April 15, 2013 9:51 AM
Monday, April 15, 2013 9:50 AM -
Wow, that is a complicated solution for this problem.
SET DATABASE TO <database.Dbc>
After that, data will get pulled from the desired tables. This assumes that your tables are in a Dbc and not free tables.
Craig Berntson
MCSD, Visual C# MVP
INETA Community Speaker
www.craigberntson.com- Proposed as answer by Ed Price - MSFTMicrosoft employee Monday, April 22, 2013 10:29 PM
- Marked as answer by Youen Zen Thursday, April 25, 2013 8:28 AM
Monday, April 15, 2013 12:33 PM -
Wow, that is a complicated solution for this problem.
SET DATABASE TO <database.Dbc>
After that, data will get pulled from the desired tables. This assumes that your tables are in a Dbc and not free tables.
Craig Berntson
MCSD, Visual C# MVP
INETA Community Speaker
www.craigberntson.comAs I had said, my solution maynot be the best.... :)
I may try this, but there may be some pitfalls. I don't remember why I didn't go this way. It was a very long time ago that I built it.
- Edited by July Derek Tuesday, April 16, 2013 6:14 AM
Tuesday, April 16, 2013 6:10 AM -
Well, the code you used finally switches SET PATH, not the database, so it's for working with FREE tables, not a DBC.
Also SET DATABASE TO somedatabase.dbc only works, if you open multiple databases and it won't close tables and repoen them, so there is more to do anyway.
If you only need to switch data at application start it's simply reading the path of the DBC from some config file and OPEN DATABASE or SET PATH or both, but if your application is already started and has open several tables, you need
CLOSE TABLES ALL
CLOSE DATABASES ALLand then OPEN DATABASE (otherpath) or SET PATH (otherpath).
You also need to ensure the database you don't want to open is not within the search path, especially if you make use of free tables and the DE and depend on the automatic search and discovery of files along all paths you set with SET PATH (ADDITIVE).
Just SET DATABASE TO is a bit oversimplyfying things. If dbc and table files are named the same, SET DATABASE just switches the active dbc, you then have to have both databases open and if you have still tables of location1 open when you switch to location2, they remain open and tables and data would mix. So that's oversimplifying the process of switching to a seperate database.
Indeed you have to explain your situation in more detail, what "the two database have the same structure" means exactly. First question is: Do you really mean a DBC with database? Because in the old fox 2.x world this also was a term for a single free table and the term is loosely used also for a directory of free tables. If so, then CLOSE/OPEN DATABASE and SET DATABASE are of no use at all.
Bye, Olaf.
Olaf Doschke (Setmics)
- Proposed as answer by Ed Price - MSFTMicrosoft employee Monday, April 22, 2013 10:29 PM
- Marked as answer by Youen Zen Thursday, April 25, 2013 8:28 AM
Tuesday, April 16, 2013 12:21 PM -
IF Native DBC OR FREE :
USE dbcname.tablename ALIAS alias1
RE. USE command
IF SQL TABLE :
CREATE SYNONYM for datasource in sp_
azizsallam
- Edited by abd el aziz sallam Saturday, April 20, 2013 9:10 PM
- Proposed as answer by Ed Price - MSFTMicrosoft employee Monday, April 22, 2013 10:29 PM
- Marked as answer by Youen Zen Thursday, April 25, 2013 8:28 AM
Saturday, April 20, 2013 9:08 PM