Tim said:
Marsh,
The function is great. I also want to know how to check
the object(s) such as table, query, form and so on..
is/are exit or not.
First, I have a problem with an application that does not
"know" what's in it. It is a very rare situation where
there's a valid reason to do check if something exists.
One common question is to check if a table exists before
deleting it. But, the answer is that it is better to delete
the records in the table rather than delete the table
object. Either that or just delete the table and ignore the
error if it doesn't exist.
Alright, to answer your question, you can just reference an
object and if there's no error, then it exists. If there's
an error, then you have to check if the error is "not in the
collection" or if there's something more serious has gone
wrong. Here's two examples, a function for queries would
be the same as the one for tables except it would use the
QueryDefs collection. Functions for Reports and Modules
would be the same as the one for forms except for using the
"Reports" and "Modules" containers.
Public Function IsTable(strItemName As String) As Variant
Dim strTemp As String
On Error GoTo ErrHandler
strTemp = CurrentDb.TableDefs(strItemName).Name
IsTable = True
ExitHere:
Exit Function
ErrHandler:
Select Case Err.number
Case 3265
IsTable = False
Case Else
MsgBox Err.number & " - " & Err.Description
IsTable = Null
End Select
Resume ExitHere
End Function
Public Function IsForm(strItemName As String) As Variant
Dim strTemp As String
On Error GoTo ErrHandler
strTemp =
CurrentDb.Containers("Forms").Documents(strItemName).Name
IsForm = True
ExitHere:
Exit Function
ErrHandler:
Select Case Err.number
Case 3265
IsForm = False
Case Else
MsgBox Err.number & " - " & Err.Description
IsForm = Null
End Select
Resume ExitHere
End Function