Microsoft Access
  Home arrow Microsoft Access arrow Page 5 - Advanced Ideas using VBA
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
MICROSOFT ACCESS

Advanced Ideas using VBA
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 5
    2007-01-04

    Table of Contents:
  • Advanced Ideas using VBA
  • 7.7 Sort an Array in VBA
  • 7.8 Fill a List Box with a List of Files
  • 7.9 Handle Object Properties, in General
  • 7.10 Detect Whether an Object Exists

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Advanced Ideas using VBA - 7.10 Detect Whether an Object Exists


    (Page 5 of 5 )

    Problem

    You create and delete objects as your application runs. At some point, you need to be able to tell whether an object exists and make decisions based on that fact. But you can't find a function in Access that will tell you if a specific object already exists. Are you missing something? This ought to be a basic part of the product!

    Solution

    You haven't missed anything: Access really doesn't supply a simple method of determining if a specific object already exists. On the other hand, this is really quite simple, as long as you understand two important concepts: Accesss support for DAO Container objects, and the ways you can use error handling to retrieve information. This solution uses these two subjects to provide a function you can call to check for the existence of any object.

    Load and run frmTestExist from 07-10.MDB. This form, shown in Figure 7-14, lets you specify an object name and its type and then tells you whether that object exists. Certainly, you wouldn't use this form as-is in any application--its purpose is to demonstrate the acbDoesObjExist function in basExists (07-10.MDB). To make your exploration of frmTestExist easier, Table 7-8 lists the objects that exist in 07-10. MDB. Try entering names that do and don't exist, and get the types right and wrong, to convince yourself that the acbDoesObjExist function does its job correctly.


    Figure 7-14.  frmTestExist lets you check for the existence of any object in the current database

    Table 7-8. The sample objects in 07-10.MDB

    Object name

    Object type

    tblTest

    Table

    qryTest

    Query

    frmTest

    Form

    frmTestExist

    Form

    basExists

    Module

    Follow these steps to use acbDoesObjExist in your own applications:

    1. Import the module basExists from 07-10.MDB. This module contains the acbDoesObjExist function.
    2. To check for the existence of any object, call acbDoesObjExist, passing to it the name of the object to check for and a value from the AcObjectType enumeration indicating the object's type. The type parameter must be chosen from the values acTable, acQuery, acForm, acReport, acMacro, or acModule. For example, to check for the existence of a table named "Customers", call acbDoesObjExist like this:

      If acbDoesObjExist("Customers", acTable) Then
         ' You know the table exists.
      Else
         MsgBox "The table 'Customers' doesn't exist!"
      End If

    Discussion

    The acbDoesObjExist function, shown in full here, checks for the existence of an object by attempting to retrieve that object's Name property. Because every object that exists exposes a Name property, this action can't fail unless the object doesn't exist. In skeleton format, the code works like this:

        Dim strName As String
        On Error Goto acbDoesObjExist_Err

        strName = obj.Name
        acbDoesObjExist = True

      acbDoesObjectExist_Exit:
         Exit Function

      acbDoesObjectExist_Err:
         acbDoesObjExist = False
         Resume acbDoesObjectExist_Exit

    That is, the code sets up an error handler and then attempts to retrieve the Name property of the requested object. If it succeeds, the code falls through, sets the return value to True, and returns. If it triggers an error, the procedure can be assured that the object doesn't exist, and it will return False.

    The only other issue is how to convert a string containing the name of the object and an integer containing its type to a real object reference. This is where the Jet engine's Container objects come in handy. The Container collections, supplied by Access so the Jet engine can support security for all the Access objects, contain collections of Document objects (one for each saved object in your database). The Containers collection contains collections named Tables, Forms, Reports, Scripts (that's macros for us users!), and Modules. Except for tables and queries, the code checks in those collections of documents, looking for the document whose name you've supplied. For tables and queries, it's simpler to use the TableDefs and QueryDefs collections directly. Access lumps tables and queries together in the Tables container, but keeps them separate in the TableDefs and QueryDefs collections. If the code looked in the Tables container, it would have to take an extra step to distinguish tables from queries; that step isn't necessary if it uses the collections instead.

    The code for acbDoesObjExist is as follows:

      Public Function acbDoesObjExist( _
      
    strObj As String, objectType As AcObjectType)
          Dim db As DAO.Database
          Dim strCon As String
          Dim strName As String

          On Error GoTo HandleErr

          Set db = CurrentDb()
          Select Case objectType
              Case acTable
                  strName = db.TableDefs(strObj).Name
              Case acQuery
                  strName = db.QueryDefs(strObj).Name
              Case acForm, acReport, acMacro, acModule
                  Select Case objectType
                      Case acForm
                          strCon = "Forms"
                      Case acReport
                          strCon = "Reports"
                      Case acMacro
                          strCon = "Scripts" 
                      Case acModule
                         
    strCon = "Modules"
                  End Select
                  strName = db.Containers(strCon).Documents(strObj).Name
          End Select
          acbDoesObjExist = True

      ExitHere:
          Exit Function
      HandleErr:
          acbDoesObjExist = False
          Resume ExitHere
     
    End Function

    Note that in the Select Case statement, the code first checks to see if you're asking about a table or a query. If so, it looks in the appropriate collection:

      Select Case objectType
         Case acTable
            strName = db.TableDefs(strObj).Name 
         Case acQuery
            strName = db.QueryDefs(strObj).Name
      .
      .
      .
      End Select

    If not, it assigns to strCon  the name of the container it will need and then attempts to retrieve the Name property of the particular document within the selected container:

      Case acForm, acReport, acMacro, acModule
         Select Case objectType
            Case acForm
               strCon = "Forms"
            Case acReport
               strCon = "Reports"
            Case acMacro
               strCon = "Scripts"
            Case acModule
              
    strCon = "Modules"
         End Select
         strName = db.Containers(strCon).Documents(strObj).Name

    See Also

    If you haven't done much investigation of DAO in Access, you may find it useful to study the appropriate chapters in the Building Applications manual that ships with Access. Though complete coverage of DAO is beyond the scope of this book, there are several examples using DAO in other chapters, especially Chapter 4 and Chapter 6. In addition, DAO Object Model: The Definitive Reference, by Helen Feddema (O'Reilly), provides complete documentation of the DAO object model.

     
    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · This article is an excerpt from the book "Access Cookbook, Second Edition,"...
     

    Buy this book now. This article is excerpted from chapter seven of the Access Cookbook, Second Edition, written by Ken Getz, Paul Litwin, and Andy Baron (O'Reilly, 2005; ISBN: 0596006780). Check it out today at your favorite bookstore. Buy this book now.

    MICROSOFT ACCESS ARTICLES

    - Converting a MySQL Database to an Excel Work...
    - Linking SQL Express 2005 Tables to MS Access...
    - Working with Access Projects in Access 2007
    - Exploring Access 2007
    - Working with Stored Procedures in an MS Acce...
    - Creating and Using Action Queries
    - Creating Data Access Pages with Charts using...
    - Advanced Ideas using VBA
    - VBA Details
    - Updating Records in MS Access
    - Using ADO`s Record Object with URLs
    - Exporting XML from MS Access 2003
    - Importing XML into MS Access 2003
    - On Using Pass-through Queries in MS Access
    - Distributed Queries in MS Access





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    Stay green...Green IT