Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

If table exists... 2

Status
Not open for further replies.

BrockLanders

Programmer
Dec 12, 2002
89
US
Hi, I want to check the current database if a certain table name exists. If so, delete that table, otherwise, run some other code. The problem I'm having is referencing the table name. Below is my code currently:
Code:
Chain = InputBox("Enter chain number.  Do not zero fill")

If CurrentDb.TableDefs(Chain) = Chain Then
    DoCmd.DeleteObject acTable, Chain
I need to have some code that says If CurrentDb.TableName = Chain Then,
however, that's not an option I don't believe.

Thanks in advance for any help
 
This is from the Code Library:
Code:
Function TableExists(strTableName As String) As Boolean
    ' This procedure returns True or False depending on whether
    ' the table named in strTableName exists.
    Dim dbs As Database, tdf As TableDef

    On Error Resume Next
    Set dbs = CurrentDb
    Set tdf = dbs.TableDefs(strTableName)
    If Err = 3265 Then
        ' Table does not exist.
        TableExists = False
    Else
        ' Table exists.
        TableExists = True
    End If
    Err = 0
End Function
 
Code:
Dim tdf As DAO.TableDef
Chain = InputBox("Enter chain number.  Do not zero fill")

On Error Resume Next
tdf = CurrentDb.TableDefs(Chain)
If Err.Number = 0 Then
   Set tdf = Nothing
   DoCmd.DeleteObject acTable, Chain
Else
   [COLOR=green]' Other Stuff[/color]

[small]No! No! You're not thinking ... you're only being logical.
- Neils Bohr[/small]
 
Thanks guys or gals (can't tell by your screen names) the code works.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top