Re: Switching Database

From: Mark (Mark_at_discussions.microsoft.com)
Date: 11/09/04


Date: Tue, 9 Nov 2004 08:58:02 -0800

Hi Andy,
I had the same problem and I added the code below to each form's
DE.BeforeOpenTable.
You have to define your own gcDrive (e.g. C:\) and your own gcPath (e.g.
Data\Backup\). If your new database has a different name you also have to
define the variable cDatabase.

Local cObjClass, cObjName, cDatabase, cNewPath, oReference
Local Array aCursors[1]

If Not (Empty(gcDrive) Or Empty(gcPath))
        = AMEMBERS(aCursors, THISFORM.Dataenvironment, 1)
        For i = 1 to ALEN(aCursors,1)
                If aCursors(i,2) = "Object"
                        cObjClass = "THISFORM.DATAENVIRONMENT." + aCursors(i,1) + ".Class"

                        If EVAL(cObjClass) = "Cursor"
                                cObjName = "THISFORM.DATAENVIRONMENT." + aCursors(i,1) + ".DATABASE"
                                cDatabase = EVAL(cObjName)
                                cNewDatabase = AllTrim(gcDrive) + AllTrim(gcPath) +
AllTrim(Substr(cDatabase, RAT("\",cDatabase) + 1))
                                oReference = EVAL("THISFORM.DATAENVIRONMENT." + aCursors(i,1))
                                oReference.Database = cNewDatabase
                                
                        Endif
                Endif
        EndFor
Endif

HTH
Mark

"Andy Trezise" wrote:

> Thanks for your help...
>
> I make sure that I don't have any forms open when I close the database
> (CLOSE DATABASES ALL). The confusing thing is that the application is
> opening a foreign database and tables when it starts up for the first time
> OK (i.e. in a different place to the development database). It's not until I
> close the database and try and reopen the archive that the problem shows up.
> If I use DBC() it shows the correct database but still opens the tables from
> the previously opened database.
>
> "Stefan Wuebbe" <stefan.wuebbe@gmx.de> wrote in message
> news:%23u5V7lbxEHA.2016@TK2MSFTNGP15.phx.gbl...
> >
> > > 1. The initial problem is that if my database is held directly under the
> > > folder from where the EXE runs and this folder has the same name as the
> one
> > > I used under development then the application seems to instinctively to
> use
> > > that database rather that the implicit one that I tell it.
> >
> > Right, there are paths stored in some of the DataEnvironment memebers'
> > properties unfortunately.
> > You can modify them either at design time / before build.
> > Or at runtime in yourForm.DE.BeforeOpenTables()
> >
> > In addition to what Olaf said, Close Databases All might fail to
> > close them actually, as long as there are any data sessions referring
> > to the DBC you want to close. IOW, you'd need to close all forms etc.
> > beforehand.
> >
> >
> > hth
> > -Stefan
> >
> >
> > "Andy Trezise" <ANDY@HOME.COM> schrieb im Newsbeitrag
> > news:ejxcJsZxEHA.3096@TK2MSFTNGP14.phx.gbl...
> > >I have an age old problem when trying to switch from one database (and
> the
> > > tables contained within it) to another during my application (VFP8). I
> just
> > > want the user to be able to switch from a LIVE database to an ARCHIVE
> > > database.
> > >
> > > 1. The initial problem is that if my database is held directly under the
> > > folder from where the EXE runs and this folder has the same name as the
> one
> > > I used under development then the application seems to instinctively to
> use
> > > that database rather that the implicit one that I tell it.
> > >
> > > 2. Whatever I try I can't seem to get my application to switch from one
> > > database to the other. The operation succeeds and DBC() always returns
> the
> > > correct database name but when I try and open a table for some reason it
> > > opens the table from my live database. I am using a hot key (ALT+A) to
> call
> > > a routine as follows:
> > >
> > > Function Switch_DB()
> > >
> > > lArchive = !lArchive
> > >
> > > IF lArchive
> > >
> > > cDB = C:\DATAFILES\MYDB.DBC
> > >
> > > ELSE
> > >
> > > cDB = C:\DATAFILES\ARCHIVES\MYDB.DBC
> > >
> > > ENDIF
> > >
> > > CLOSE ALL DATABASE
> > >
> > > OPEN DATABASE &cDB
> > >
> > > SET DATABASE TO &cDB
> > >
> > > RETURN
> > >
> > >
> >
>
>
>