PC Review


Reply
Thread Tools Rate Thread

Check if objects exist

 
 
Pietro
Guest
Posts: n/a
 
      15th Nov 2008
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
 
Reply With Quote
 
 
 
 
James A. Fortune
Guest
Posts: n/a
 
      16th Nov 2008
> 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....c22f9250f9465e

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 Removed)
 
Reply With Quote
 
John Spencer
Guest
Posts: n/a
 
      16th Nov 2008
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
'====================================================


Pietro wrote:
> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Check a collection for objects that may or may not exist within it NateBuckley Microsoft Excel Programming 2 14th May 2008 01:50 PM
check SQL db exist... =?Utf-8?B?Sm9u?= Microsoft VB .NET 3 17th May 2005 12:57 PM
Question on ToggleBox / CheckBox objects knowing where they exist in a sheet Sean O'Neill Microsoft Excel Programming 5 14th Mar 2004 12:33 PM
How to test for objects property that does not exist Steve Montague Microsoft C# .NET 3 24th Feb 2004 07:55 AM
How to references objects that exist on local or network drives that aren't open. Mike-hime Microsoft Excel Programming 0 7th Jan 2004 10:15 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 03:31 AM.