Hi,
What's the shortest code to check the following:
1-If a table called Table1 exists or not.
2-If a query called Query1 exists or not.
3-If a form called Form1 exists or not.
an [sic] if any of those exist, they should be deleted
For forms, a very robust way, but not necessarily the shortest way is to
adapt the code from:
http://groups.google.com/group/comp.databases.ms-access/msg/dfc22f9250f9465e
Remember to ...
Here.
Public Function DoesFormExist(strFormName As String) As Boolean
Dim MyDB As DAO.Database
Dim MyForm As Document
Dim ctr As Container
DoesFormExist = False
Set MyDB = CurrentDb
For Each ctr In MyDB.Containers
If ctr.Name = "Forms" Then
For Each MyForm In ctr.Documents
If strFormName = MyForm.Name Then
DoesFormExist = True
Set MyDB = Nothing
Exit Function
End If
Next MyForm
End If
Next ctr
Set MyDB = Nothing
End Function
Although the Containers collection can be used for tables and queries as
well, I would use the TableDefs collection and the QueryDefs collection
in a similarly robust way. Others might try TableDefs(strTableName) and
conclude that the table doesn't exist if that reference causes an error.
Note that the TableDefs collection and the QueryDefs collection each
contain a Delete method and the TableDef object and the QueryDef object
each contain a Name property.
James A. Fortune
(e-mail address removed)