Logo

Hacker’s Guide to Visual FoxPro
An irreverent look at how Visual FoxPro really works. Tells you the inside scoop on every command, function, property, event and method of Visual FoxPro.

SET DATABASE, DBC(), Set(“Database”)

SET DATABASE lets you specify which database should be current, while DBC() and SET(“DATABASE”) tell you which database is currently selected.

Usage

SET DATABASE TO [ Name ]

In VFP 7, if the database has Database Events turned on, the Activate event fires. If another database was current and it has Database Events turned on, its Deactivate event fires first.

If you have two databases open with the same name (but in different directories, obviously), be aware of a potential “gotcha”: Specifying the name without a path in the SET DATABASE command selects the most recently opened database.

Omitting Name deselects the currently selected database without selecting another.

Why would you want to select a particular database anyway? There are some commands and functions (like ADD TABLE, MODIFY DATABASE, PACK DATABASE, and DBGetProp()) that require the database to be current, and others (like CLOSE DATABASES) that behave differently when no database is selected. Opening a table in a database opens the database, but doesn’t make it current. If you want to use DBGetProp() or one of the other DBC-dependent functions, you need the database to be current.

You might expect that stored procedures need the database to be current, but they don’t. For example, if the Default property for a field calls a stored procedure, VFP has no problem finding and executing that procedure, even when the database isn’t the current one. One thing to be aware of, however, is that the database the stored procedure is executed from isn’t automatically made the current one. The DBC() function called in a stored procedure returns the name of the current database, which isn’t necessarily the database the stored procedure is in. So, if a stored procedure in the non-current database tries to open a table without specifying the database name, either the wrong table will be opened (if there’s a table with that same name in the current database) or you’ll get an error saying that the table doesn’t exist.

Some commands (like CLOSE DATABASES) can leave you with no current database.

Example

OPEN DATABASE \Test1\MyData
OPEN DATABASE \Test2\MyData
SET DATABASE TO
SET DATABASE TO MyData
? DBC()         && Returns "H:\TEST2\MYDATA.DBC"

Usage

cDatabase = DBC()
cDatabaseStemOnly = SET( "DATABASE" )

DBC() returns the name of the current database. If FULLPATH is ON, the return value includes the complete path to the database. If FULLPATH is OFF, it returns only the drive and filename. If no database is current, DBC() returns the empty string. SET(“DATABASE”) returns only the name portion of the current database. That is, it omits the drive, path and extension.

Example

OPEN DATABASE MyData
? DBC()         && Returns "H:\HACKER\TEST\MYDATA.DBC"
? SET("DATABASE")  && Returns "MYDATA"
OPEN DATABASE MoreData
? DBC()         && Returns "H:\HACKER\TEST\MOREDATA.DBC
? SET("DATABASE")  && Returns "MOREDATA"
SET DATABASE TO
? DBC()         && Returns ""
? SET("DATABASE")  && Returns ""
CLOSE DATABASES
* All open free tables are closed
SET FULLPATH OFF
SET DATABASE TO MyData
? DBC()         && Returns "H:MYDATA.DBC"
CLOSE DATABASES
? DBC()         && Returns ""

See Also

Activate Database Event, ADatabases(), Close Database, Create Database, Deactivate Database Event, Delete Database, Modify Database, Open Database, Pack Database, Validate Database