Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

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


View products that this article applies to.

This article was previously published under Q210398
Advanced: Requires expert coding, interoperability, and multiuser skills.

↑ Back to the top


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.

↑ Back to the top


More information

Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers 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 requirements. NOTE: The sample code in this article uses Microsoft Data Access Objects. For this code to run properly, you must reference the Microsoft DAO 3.6 Object Library. To do so, click References on the Tools menu in the Visual Basic Editor, and make sure that the Microsoft DAO 3.6 Object Library check box is selected.

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:
    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 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
    					
  4. 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).
  5. Try using other valid database names and other table and query names in the line above.

↑ Back to the top


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.

↑ Back to the top


Keywords: KB210398, kbprogramming, kbofficeprog, kbhowto, kbdta

↑ Back to the top

Article Info
Article ID : 210398
Revision : 4
Created on : 10/11/2006
Published on : 10/11/2006
Exists online : False
Views : 407