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.

ALIAS(), DBF(), SELECT(), USED()

These functions return an assortment of information about work areas and the tables in use in them. (Why does this sound like the promo for a trashy talk show? “Work areas and the tables that use them—next Geraldo!”)

Several of these functions have an optional parameter that can be either cAlias or nWork area. If either is passed, the function returns information about the table in use in the specified work area (the one where cAlias is the alias or the one numbered nWork area). When this parameter is omitted, these functions return information about the current work area.

Usage

cAliasUsed = ALIAS( [ cAlias | nWorkArea ] )

ALIAS() tells you the alias of the table open in the specified work area. We find it hard to imagine why anyone would ever pass cAlias to ALIAS(). If you already know the alias for the work area, why are you asking? However, it does mean you can write something like ?ALIAS(ALIAS()), which at least provides comic relief.

If there’s no table in use in the specified work area, ALIAS() returns the empty string.

Example

cInUse = ALIAS()

Usage

cFileName = DBF( [ cAlias | nWorkArea ] )

DBF() tells you the name of the table open in the specified work area. In this case, specifying an alias makes lots of sense. You provide the alias and DBF() tells you what table it’s an alias for. If no table is in use in the specified work area, you get the empty string.

DBF() is sensitive to SET FULLPATH. When FULLPATH is ON, DBF() returns a fully qualified path and filename. With FULLPATH OFF, DBF() returns just a drive and filename.

DBF() provides a way of distinguishing “real” cursors from those that are just filters of the original table. When FoxPro can get away with it, the cursor created by a SELECT-SQL is simply a filter of the table in the FROM clause. (You can prevent this behavior with the NOFILTER clause.) In that case, DBF(“the cursor alias”) returns the name of the original table. When a query creates a real cursor, DBF() returns the name of a temp file—a bunch of digits with a TMP extension.

Example

IF RIGHT(DBF(),3)="TMP"
   * it's a "real" cursor
ELSE
   * it's a filter
ENDIF

It’s most important to distinguish between a real cursor and a filter when you want to use APPEND FROM to add the records in a cursor to another table. You have to APPEND FROM DBF(“the cursor”). If the cursor is really just a filter, doing so adds all the records of the original rather than just the selected records. Be aware, however, that just because DBF() returns a .TMP name, the file does not actually have to be on the disk. VFP often buffers the contents of a small result set in memory and never writes them to disk. Consider COPY TO if you need to have data with a disk presence.

Usage

nWorkArea = SELECT( [ nDoWhat | cAlias ] )

Parameter

Value

Meaning

nDoWhat

0

Return the number of the current work area.

Omitted

If cAlias is also omitted, return the number of the current work area.

1

Return the number of the highest available work area.

Anything else

Return 0.

cAlias

Character

Return the number of the work area where cAlias is open.

Omitted

If nDoWhat is also omitted, return the number of the current work area.

SELECT() is a somewhat confused function—it has three related, yet quite different meanings. It can tell you the number of the current work area, or the work area where a table is open, or it can find an unused work area for you.

The 0 value for nDoWhat is unnecessary; omitting the parameter altogether has the same result.

SELECT(1) isn’t all that useful anymore since SELECT 0 lets you find an available work area and USE … IN 0 opens a table in an available area. We haven’t found any reasons to want the highest rather than the lowest available work area—except, perhaps, to show off the 32,767 areas available in each data session.

SELECT() is especially useful when you need to change work areas in a black-box function. You can save the current work area, go do what you need to, then restore the work area before leaving the function. Other than this, we don’t have any use for SELECT(), since we don’t want to know what work area a table uses.

Example

LOCAL nSelect
* Save the work area
nSelect=SELECT()
* Now go to another
SELECT 0
USE SomeTable
* Do something
USE
* Return to original work area
SELECT (nSelect)

Usage

lInUse = USED( [ cAlias | nWorkArea ] )

USED() tells you whether a particular work area or alias is in use. With no parameter, it tells you whether there’s a table open in the current work area. With nWork area, it tells whether the specified work area is in use.

When you pass an alias, it tells you whether any table is open with that alias. Notice that this is not the same as asking if a particular table is in use—this command checks aliases, not tables.

USED() is most handy to see whether you’ve already opened a particular table (and given it a particular alias). For example, you might use it to check whether a lookup table has already been opened.

Example

IF USED('lookups')
   SELECT lookups
ELSE
   SELECT 0
   USE lookups
ENDIF

See Also

Alias, DBC(), DBUsed(), InDBC(), Select, Set Database, Set FullPath, Use