Check if objects exist

  • Thread starter Thread starter Pietro
  • Start date Start date
P

Pietro

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 if any of those exist, they should be deleted
 
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)
 
The shortest code might be to just delete the object and ignore the error.

'====================================================
John Spencer
Access MVP 2002-2005, 2007-2008
The Hilltop Institute
University of Maryland Baltimore County
'====================================================
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top