Microsoft KB Archive/210398

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

Article ID: 210398

Article Last Modified on 10/11/2006

-

APPLIES TO


 * Microsoft Access 2000 Standard Edition

-



This article was previously published under Q210398



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. 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:  Open the sample database Northwind.mdb.  Create a module and type the following line in the Declarations section if it isn't already there: Option Explicit   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   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).  Try using other valid database names and other table and query names in the line above.

<div class="references_section">