ACC: How to Determine If a Table or Query Exists (113549)



The information in this article applies to:

  • Microsoft Access 2.0
  • Microsoft Access for Windows 95 7.0
  • Microsoft Access 97

This article was previously published under Q113549

SUMMARY

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

This article contains a sample user-defined function named 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."

Modification Type:MinorLast Reviewed:10/11/2006
Keywords:kbhowto kbProgramming KB113549