Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I test to see if an object Exists in the database

Access Howto:

How can I test to see if an object Exists in the database

by  jimmythegeek  Posted    (Edited  )
This is an awsome function to determine if any object (table, query, form, report, macro, or module) exists in a database. The IsLoaded function tells you if a form is open, but this function tells you if an object EXISTS.

It is extremely simple to use: Just copy the function into a module and your ready to go.

The function returns a true or false whether the object that you passed exists or not. To use it, Simply pass it what type of object, using the Access intrinsic constants (acTable, acQuery, acForm, acReport, acMacro, acModule) and the string name of the object, for example:

If ObjectExists(acTable, "tblOrders") Then ..... OR

If ObjectExists(acQuery, "CashLtrRpt") Then DoCmd.DeleteObject acQuery, "CashLtrRpt"

That's it! I hope you enjoy it, I have used it hundreds of times. I wrote it when I could not find anything similar looking in every resource I could find. Let me know if it helps you.

Jim Lunde
jimlunde@gmail.com

======================

Function ObjectExists(ObjType As Integer, ObjName As String) As Boolean
'Purpose: Determines whether or not a given object exists in database
'Example: If ObjectExists(acTable, "tblOrders") then ...

On Error Resume Next
Dim strTemp As String, strContainer As String

Select Case ObjType
Case acTable
strTemp = CurrentDB.TableDefs(ObjName).NAME
Case acQuery
strTemp = CurrentDB.QueryDefs(ObjName).NAME
Case acMacro, acModule, acForm, acReport
Select Case ObjType
Case acMacro
strContainer = "Scripts"
Case acModule
strContainer = "Modules"
Case acForm
strContainer = "Forms"
Case acReport
strContainer = "Reports"
End Select
strTemp = CurrentDB.Containers(strContainer).Documents(ObjName).NAME
End Select

ObjectExists = (Err.Number = 0)
End Function
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top