How to Obtain a List of Open .DBF Files Programmatically

Last reviewed: June 27, 1995
Article ID: Q107361
The information in this article applies to:
  • Microsoft FoxPro for Windows, versions 2.5x, 2.6, 2.6a
  • Microsoft FoxPro for MS-DOS, versions 2.0, 2.5x, 2.6, 2.6a
  • Microsoft FoxPro for Macintosh, versions 2.5x, 2.6a

SUMMARY

Using the program below, you can obtain a list of the open database files in a program for use in a list box, backup routine, or other procedure.

MORE INFORMATION

The following program dimensions an array with the maximum number of open work areas for the version of FoxPro currently running. The maximum number of work areas is 25 for FoxPro 2.0 and the Standard (16-bit) version of FoxPro 2.5 for MS-DOS. For all other versions of FoxPro later than 2.0, the maximum number of work areas is 225.

Each work area is selected and tested for an open file using the DBF() function. If the DBF() function returns a name, the filename is placed in an array that can be used in other procedures. The array will contain two columns. The first column will contain the .DBF file name and the second will contain the work area in which the .DBF was opened.

   * Variables Used
   * maxwarea     - Maximum number of work areas, dependent on FoxPro
   *                version being run
   * tablst       - Array containing list of open tables
   * wactr        - Counter to determine how many work areas
   *                contain an open file
   * i            - Incremental counter used in for loop/currently
   *                selected work area

   maxwarea = select(1)  && Determine max number of workareas available

   DIMENSION tablst(1,3)
   CLEAR

   * Begin at the lowest work area and initialize counter
   SELECT 1
   wactr=0

   FOR i = 1 to maxwarea
      SELECT (i)

      * If a .DBF is open, add the name and work area number
      * to the next array row
      IF !EMPTY(DBF())
         wactr = wactr + 1
         DIMENSION tablst(wactr,3)
         tablst(wactr,1) = DBF()
         tablst(wactr,2) = i
         tablst(wactr,3) = ALIAS()
      ENDIF
   ENDFOR

   CLOSE ALL

   IF TYPE("tablst(1,1)") = "L"  && Return if no tables were open
       RETURN
   ENDIF

   * Reopen tables in the right workareas
   FOR i = 1 to ALEN(tablst,1)
      SELECT (tablst(i,2))
      USE (tablst(i,1)) ALIAS (tablst(i,3)) AGAIN
   ENDFOR

   DISPLAY MEMORY LIKE tablst


Additional reference words: FoxMac FoxDos FoxWin 2.00 2.50 2.50a 2.50b
2.50c 2.60
2.60a
work area
KBCategory: kbenv kbprg kbcode
KBSubcategory: FxenvMemory


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: June 27, 1995
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.