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.

ASESSIONS()

This is a function that was sorely missed in VFP 6 and earlier versions. Added in VFP 7, it fills an array with the list of data sessions in use.

Usage

nSessionCount = ASESSIONS( aSessionList )

Parameter

Value

Meaning

aSessionList

Array

An array to hold the list of sessions. The array is created if it doesn't already exist and resized if it does. The resulting array is one-dimensional.

nSessionCount

Numeric

The number of open data sessions.

Every now and then, you need to go through all the open data sessions to do some kind of task, such as closing all open tables and rolling back open transactions (say, in an error situation) or closing one table wherever it’s open. In older versions of VFP, this was difficult because there was no way to know how many data sessions were in use and what data session numbers they were assigned. You could loop, issuing SET DATASESSION until you found an unused data session number (catching the resulting error), but due to the way data session numbers are assigned, that didn’t guarantee visiting every session.

No more. ASESSIONS() does for data sessions what AUSED() does for work areas: It fills an array with the list of data sessions in use and returns the count.

At first glance, you might wonder why it isn’t enough to just be able to get the number of open data sessions. After all, they’re assigned numbers, starting with 1 for the default, public, always active data session. However, once you create a data session, it keeps its session number, even if data sessions created earlier are closed. So there can be gaps in the list. For example, suppose you open three forms with private data sessions—they’re assigned session numbers 2, 3 and 4. If you then close the first of those forms, the list of data sessions in use is 1 (the default), 3 and 4. Issuing ASESSIONS() at this point would return 3 and create a three-element array containing 1, 3 and 4.

To make matters more interesting, VFP always fills in holes in the sequence, if possible. So, continuing the previous example, if you run another form at this point, it’s assigned data session 2. Issuing ASESSIONS() at this point returns 4 and creates a four-element array with the values 1, 3, 4 and 2.

Example

* You're most likely to use the results of
* ASessions() to drive a loop.
LOCAL nOldSession
LOCAL nSessionCount, aSessionList[1], nSession
LOCAL nTableCount, aTables[1], nTable

nOldSession = SET("DATASESSION")
nSessionCount = ASESSIONS( aSessionList )

FOR EACH nSession in aSessionList
   * Switch sessions
   SET DATASESSION TO nSession
   * Get a list of open tables.
   nTableCount = AUSED( aTables )
   * Do something for each data session, like ROLLBACK
   FOR nTable = 1 TO nTableCount
      * Do something with each table, like TABLEREVERT().
      WAIT WINDOW ALIAS( nTable )
   ENDFOR
ENDFOR

* Return to original data session
SET DATASESSION TO (nOldSession)

See Also

ADatabases(), Array Manipulation, AUsed(), Set DataSession