Wrox Programmer Forums
|
Access VBA Discuss using VBA for Access programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 25th, 2005, 10:37 AM
Registered User
 
Join Date: Mar 2005
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default Determine if a table exist

Hi! I am creating a database that needs to determine if a certain table already exist or not. How do I do this?

Thank you.

 
Old March 25th, 2005, 12:52 PM
Friend of Wrox
 
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

You can select the table, and see if that results in an error (using the VBA methods to select an item in the database window), or you can loop through the TableDefs collection, checking the name for each item against the name that you are after.
 
Old March 25th, 2005, 09:12 PM
Friend of Wrox
 
Join Date: Nov 2004
Posts: 248
Thanks: 0
Thanked 1 Time in 1 Post
Default

Try something like this:

Code:
If CurrentDb().TableDefs(strTableName).Name = strTableName Then
    ' nop - just doing this if to see if Err 3265 occurs
End If
If Err.Number = 0 then
    ' table exists
ElseIf Err.Number = 3265 Then ' "Item Not Found in the collection"
    ' table does not exist
    Err.Clear
    Debug.Print "table does not exist"
Else
    ' unhandled error
    Err.Raise
End If
Randall J Weers
Membership Vice President
Pacific NorthWest Access Developers Group
http://www.pnwadg.org
 
Old March 27th, 2005, 06:53 AM
Authorized User
 
Join Date: Jul 2003
Posts: 73
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Try the following function - hope it helps

Function Gen_test_if_table_exists(TableName) As Boolean

   Dim tdfLoop
   Gen_test_if_table_exists = False
   With CurrentDb
        'Debug.Print .TableDefs.Count & " TableDefs in " & .Name

        ' Enumerate TableDefs collection.
      For Each tdfLoop In .TableDefs
           'Debug.Print " " & tdfLoop.Name
         If TableName = tdfLoop.Name Then
            Gen_test_if_table_exists = True
            Exit Function
         End If
      Next tdfLoop

    End With

End Function

Cheers Ray

Cheers Ray
 
Old March 30th, 2005, 04:40 AM
Registered User
 
Join Date: Mar 2005
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi guys! Thanks for the help.
I will try it out.

Have a nice day.






Similar Threads
Thread Thread Starter Forum Replies Last Post
Return a record that doesn't exist in a table? Unregistered SQL Server 2000 4 May 3rd, 2006 01:27 AM
Table Exist? elansolutionsltd Access VBA 5 January 23rd, 2006 03:43 AM
Error :Provider (0x80040E37)Table does not exist. thahir Classic ASP Components 0 September 12th, 2005 12:02 AM
Table 'buzzly_comicsite.forum_admin' doesn't exist AMP_Engineer BOOK: Beginning PHP, Apache, MySQL Web Development ISBN: 978-0-7645-5744-6 1 September 17th, 2004 12:01 PM
Table 'buzzly_comicsite.forum_admin' doesn't exist AMP_Engineer BOOK: Beginning PHP, Apache, MySQL Web Development ISBN: 978-0-7645-5744-6 0 September 4th, 2004 06:04 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.