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 Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Check to see if a table exists. If so delete it. 2

Status
Not open for further replies.

rstitzel

MIS
Apr 24, 2002
286
US
I need a function that will check the existence of a table. For example tbl_employeemaster. If tbl_employeemaster already exists I want to delete the table from the current database.

Thanks in advance for any help.

 
Dim tdf as TableDef
For Each tdf in CurrentDb.TableDefs
If tdf.Name = "tbl_employeemaster" Then
DoCmd.DeleteObject tdf.Name
End If
Next

That should do it.

Paul
 
Sorry there are two arguments for this method.
DoCmd.DeleteObject acTable, tdf.Name

Paul
 
Here is a function that will tell you if a table exists:
Code:
Public Function TableExists(strTableName As String) As Integer
Dim db As Database
Dim i As Integer
    Set db = CurrentDb
    TableExists = False
    db.TableDefs.Refresh
    For i = 0 To db.TableDefs.count - 1
        If strTableName = db.TableDefs(i).Name Then
            'Table Exists
            TableExists = True
            Exit For
        End If
    Next i
    Set db = Nothing
End Function
And you can use it like this:
Code:
If TableExists("tblEmployeeMaster") Then db.Execute "DROP TABLE tblEmployeeMaster"
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top