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