ACC: How to Determine If a Table or Query Exists

Last reviewed: August 29, 1997
Article ID: Q113549
The information in this article applies to:
  • Microsoft Access 2.0, 7.0, 97

SUMMARY

Advanced: Requires expert coding, interoperability, and multiuser skills.

This article describes a sample user-defined function, IsTableQuery(), that you can use to determine whether a table or query exists in a database. The sample function uses the TableDefs and QueryDefs data access objects (DAO) collections.

This article assumes that you are familiar with Visual Basic for Applications and with creating Microsoft Access applications using the programming tools provided with Microsoft Access. For more information about Visual Basic for Applications, please refer to your version of the "Building Applications with Microsoft Access" manual.

NOTE: Visual Basic for Applications is called Access Basic in Microsoft Access version 2.0. For more information about Access Basic, please refer to the "Building Applications" manual.

MORE INFORMATION

The following example demonstrates how to use the sample user-defined function IsTableQuery() to determine whether a table or query exists in a database:

  1. Open the sample database Northwind.mdb (or NWIND.MDB in version 2.0).

  2. Create a module and type the following line in the Declarations section if it isn't already there:

          Option Explicit
    

  3. Type the following procedure:

          '********************************************************
          ' FUNCTION: IsTableQuery()
          '
          ' PURPOSE: Determine if a table or query exists.
          '
          ' ARGUMENTS:
    
          '   DbName: The name of the database. If the database name
          '           is "" the current database is used.
          '    TName: The name of a table or query.
          '
          ' RETURNS: True (it exists) or False (it does not exist).
          '
          '********************************************************
          Function IsTableQuery(DbName As String, TName As String) As Integer
             Dim Db As Database, Found As Integer, Test As String
             Const NAME_NOT_IN_COLLECTION = 3265
    
             ' Assume the table or query does not exist.
             Found = False
    
             ' Trap for any errors.
             On Error Resume Next
    
             ' If the database name is empty...
             If Trim$(DbName) = "" Then
                ' ...then set Db to the current Db.
                Set Db = CurrentDb()
             Else
                ' Otherwise, set Db to the specified open database.
                Set Db = DBEngine.Workspaces(0).OpenDatabase(DbName)
    
                ' See if an error occurred.
                If Err Then
                   MsgBox "Could not find database to open: " & DbName
                   IsTableQuery = False
                   Exit Function
                End If
             End If
    
             ' See if the name is in the Tables collection.
             Test = Db.TableDefs(TName).Name
             If Err <> NAME_NOT_IN_COLLECTION Then Found = True
    
             ' Reset the error variable.
             Err = 0
    
             ' See if the name is in the Queries collection.
             Test = Db.QueryDefs(TName$).Name
             If Err <> NAME_NOT_IN_COLLECTION Then Found = True
    
             Db.Close
    
             IsTableQuery = Found
    
          End Function
    
    

  4. To test this function, type the following line in the Debug window (or Immediate window in version 2.0), and then press ENTER.

           ?IsTableQuery("","Invoices")
    

    Note that a "-1" is displayed indicating that the Invoices query was found (Microsoft Access stores a "-1" for True and a "0" for False).

  5. Try using other valid database names and other table and query names in the line above.

REFERENCES

For more information about Tabledefs, search the Help Index for "Tabledefs."

For more information about Querydefs, search the Help Index for "Querydefs."

Keywords          : kbprg PgmHowTo PgmObj
Version           : 2.0 7.0 97
Platform          : WINDOWS
Hardware          : x86
Issue type        : kbhowto


================================================================================


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: August 29, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.