ACC2000: Sample Code to Check for Table or Query in a Database

ID: Q210398


The information in this article applies to:
  • Microsoft Access 2000

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


SUMMARY

This article contains a sample user-defined function named IsTableQuery() that you can use to determine whether a table or a query exists in a database. The sample function uses the TableDefs and QueryDefs Data Access Objects (DAO) collections.


MORE INFORMATION

Microsoft provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support professionals can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific needs. If you have limited programming experience, you may want to contact a Microsoft Certified Solution Provider or the Microsoft fee-based consulting line at (800) 936-5200. For more information about Microsoft Certified Solution Providers, please see the following page on the World Wide Web:

http://www.microsoft.com/mcsp/
For more information about the support options available from Microsoft, please see the following page on the World Wide Web:

http://www.microsoft.com/support/supportnet/overview/overview.asp
The sample code in this article uses Microsoft Data Access Objects. For this code to run properly, you need to reference the Microsoft DAO 3.6 Object Library.

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.


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


  3. 
    Option Explicit 
  4. Type the following procedure:


  5. 
    '********************************************************
    ' 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 DAO.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 
  6. To test this function, type the following line in the Immediate window, 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).


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



REFERENCES

For more information about the TableDefs collection, in the Visual Basic Editor, click Microsoft Visual Basic Help on the Help menu, type "TableDefs collection" in the Office Assistant or the Answer Wizard, and then click Search to view the topic.

For more information about the QueryDefs collection, in the Visual Basic Editor, click Microsoft Visual Basic Help on the Help menu, type "QueryDefs collection" in the Office Assistant or the Answer Wizard, and then click Search to view the topic.

Additional query words: inf how to determine exists

Keywords : kbprg kbdta AccCon PgmObj
Version : WINDOWS:2000
Platform : WINDOWS
Issue type : kbhowto


Last Reviewed: July 6, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.