You are currently offline, waiting for your internet to reconnect

ACC: How to Determine If a Table or Query Exists

This article was previously published under Q113549
Retired KB Content Disclaimer
This article was written about products for which Microsoft no longer offers support. Therefore, this article is offered "as is" and will no longer be updated.
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 adatabase. The sample function uses the TableDefs and QueryDefs Data AccessObjects (DAO) collections.

This article assumes that you are familiar with Visual Basic forApplications and with creating Microsoft Access applications using theprogramming tools provided with Microsoft Access. For more informationabout 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 MicrosoftAccess version 2.0. For more information about Access Basic, please referto the "Building Applications" manual.
MORE INFORMATION
The following example demonstrates how to use the sample user-definedfunction IsTableQuery() to determine whether a table or query exists in adatabase:

  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."
Properties

Article ID: 113549 - Last Review: 01/18/2007 23:40:00 - Revision: 2.3

  • Microsoft Access 2.0 Standard Edition
  • Microsoft Access 95 Standard Edition
  • Microsoft Access 97 Standard Edition
  • kbhowto kbprogramming KB113549
Feedback