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: How to Check Whether an Object Exists in a Database


View products that this article applies to.

Summary

This article shows you how to create a sample user-defined function to check whether an object exists in the current database. This can be useful before you create a new object (such as a table, a form, or a report) in the database.

↑ 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.

How to Create the Function

Check for the existence of an object in the current database by using the ObjectType argument to determine the type of object to be retrieved (tables, queries, forms, reports, macros, and modules) and the ObjectName argument to represent the name of the specified object type.

The function will return:
True (-1), if the object exists.
False (0), if the object does not exist.
CAUTION: If you follow the steps in this example, you modify the sample database Northwind.mdb. You may want to back up the Northwind.mdb file and follow these steps on a copy of the database.

To create the function follow these steps:
  1. Open the sample database Northwind.mdb.
  2. On the Insert menu, click Module.
  3. Type the following code into the new module:
    
    Function DoesObjectExist (ObjectType$, ObjectName$)
    On Error Resume Next
    
    Dim Found_Object, Find_Object As String, ObjectNum As Integer
    Dim DB As Database, T As TableDef
    Dim Q As QueryDef, C  As Container
    Dim Msg As String
    Found_Object = -1
    Set DB = dbengine(0)(0)
    
    Select Case ObjectType$
    Case "Tables"
    
        Find_Object = DB.TableDefs(ObjectName$).Name
    
    Case "Queries"
    
        Find_Object = DB.QueryDefs(ObjectName$).Name
    
    Case Else
    
        If ObjectType$ = "Forms" Then
            ObjectNum = 1
        ElseIf ObjectType$ = "Modules" Then
            ObjectNum = 2
        ElseIf ObjectType$ = "Reports" Then
            ObjectNum = 4
        ElseIf ObjectType$ = "Macros" Then
            ObjectNum = 5
        Else
             Msg = "Object Name """ & ObjectType & """ is an invalid"
             Msg = Msg & " argument to function ObjectExists_20!"
             MsgBox Msg, 16, "ObjectExists_20"
             Exit Function
    
        End If
    
        Set C = DB.Containers(ObjectNum)
        Find_Object = C.Documents(ObjectName$).Name
    
    End Select
    
    If Err = 3265 Or Find_Object = "" Then
    
        Found_Object = 0
    
    End If
    
    DoesObjectExist = Found_Object
    
    End Function
    					

How to Use the Function

  1. Open the module containing the function in Design view, and on the View menu, click Immediate Window.
  2. Type the following line in the Immediate window, and then press ENTER:
    ?DoesObjectExist("Tables","Employees")
    					
If a table named "Employees" exists in the current database, - 1 is returned. If no table named Employees exists, 0 is returned.

↑ Back to the top


Keywords: KB210598, kbprogramming, kbhowto

↑ Back to the top

Article Info
Article ID : 210598
Revision : 3
Created on : 6/23/2005
Published on : 6/23/2005
Exists online : False
Views : 337