You are currently offline, waiting for your internet to reconnect

ACC2000: How to Check Whether an Object Exists in a Database

This article was previously published under Q210598
This article has been archived. It is offered "as is" and will no longer be updated.
Moderate: Requires basic macro, coding, and interoperability skills.

This article applies only to a Microsoft Access database (.mdb).

This article shows you how to create a sample user-defined function tocheck 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.
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 NextDim Found_Object, Find_Object As String, ObjectNum As IntegerDim DB As Database, T As TableDefDim Q As QueryDef, C  As ContainerDim Msg As StringFound_Object = -1Set DB = dbengine(0)(0)Select Case ObjectType$Case "Tables"    Find_Object = DB.TableDefs(ObjectName$).NameCase "Queries"    Find_Object = DB.QueryDefs(ObjectName$).NameCase 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$).NameEnd SelectIf Err = 3265 Or Find_Object = "" Then    Found_Object = 0End IfDoesObjectExist = Found_ObjectEnd 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:
If a table named "Employees" exists in the current database, - 1 isreturned. If no table named Employees exists, 0 is returned.

Article ID: 210598 - Last Review: 12/05/2015 11:35:08 - Revision: 3.0

  • Microsoft Access 2000 Standard Edition
  • kbnosurvey kbarchive kbhowto kbprogramming KB210598