Test for the existence of a table 
Author Message
 Test for the existence of a table

Try to select it in the database window...

DoCmd.SelectObject acForm, "Customers", True
If it fails, it's not there.  :)

:>Wes

Quote:
> How does one test for the existence of a table (or any other object, for
> that matter) before performing a given operation in a module?



Sun, 19 Sep 1999 03:00:00 GMT  
 Test for the existence of a table

Quote:

> Any suggestions would be greatly appreciated:

> How does one test for the existence of a table (or any other object, for
> that matter) before performing a given operation in a module?

> I've written a module that deletes tables and imports new ones from an
> external database.  However, if the original table does not exist, I can't
> seem to properly code an error handling routine.

> Regards,
> Michael

Function DoesTableExist(DatabaseName As String, TableName As String) As Boolean
    Dim dbs As Database, i As Integer
    DoesTableExist = False
    Set dbs = DBEngine.Workspaces(0).OpenDatabase(DatabaseName)
    For i = 0 To dbs.TableDefs.count - 1
    If UCase(TableName) = UCase(dbs.TableDefs(i).Name) Then
        DoesTableExist = True
        Exit For
    End If
    Next i
    dbs.Close
    Set dbs = Nothing
End Function

Sub KillTable(DatabaseName as String, TableName As String)
    Dim dbs As Database, i As Integer
    Set dbs = DBEngine.Workspaces(0).OpenDatabase(DatabaseName)
    For i = 0 To dbs.TableDefs.count - 1
    If UCase(TableName) = UCase(dbs.TableDefs(i).Name) Then
        dbs.TableDefs.Delete TableName
        Exit For
    End If
    Next i
    dbs.Close
    Set dbs = Nothing
End Sub

Regards,

S.Y.



Mon, 20 Sep 1999 03:00:00 GMT  
 Test for the existence of a table

Quote:

> Any suggestions would be greatly appreciated:

> How does one test for the existence of a table (or any other object, for
> that matter) before performing a given operation in a module?

> I've written a module that deletes tables and imports new ones from an
> external database.  However, if the original table does not exist, I can't
> seem to properly code an error handling routine.

> Regards,
> Michael

Michael

If you get the err code for a table not existing, lets say its XXXX.
When you trap an err ("on error goto function_ERR" at the beginning of
your function) use this:

function_ERR:
        if ERR = XXXX then
          Resume next
        else
          msgbox ERR & "  " & Error$
        end if

the function will continue at the next line.
hope this helps

matt.



Mon, 20 Sep 1999 03:00:00 GMT  
 Test for the existence of a table

Hi Michael,

If the object is part of a collection, try to obtain some information about
this object, from the collection. If that fails, you may assume the object
don't exist:

Dim Db As Database ' Somewhere
.....
Public Function IsTable(strName) As Boolean
If Db Is Nothing Then Set Db=CurrentDb( )
        On Error Resume Next
        IsTable= ( strName = Db.TableDefs( strName) .name )
        If err.Number then IsTable = False
        Exit Function
End Function



Quote:
> Any suggestions would be greatly appreciated:

> How does one test for the existence of a table (or any other object, for
> that matter) before performing a given operation in a module?

> I've written a module that deletes tables and imports new ones from an
> external database.  However, if the original table does not exist, I
can't
> seem to properly code an error handling routine.

> Regards,
> Michael



Tue, 21 Sep 1999 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Testing for table existence & listing available tables

2. Testing for Existence of Table

3. Test for table existence

4. Testing for table existence

5. Testing for table existence

6. Testing for table existence

7. Testing for table existence

8. Testing the existence of a table

9. TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST

10. Testing for existence of a query

11. Testing for a record already in existence

12. test for existence of a field

 

 
Powered by phpBB® Forum Software