how to delete a table if it exists

  • Thread starter Thread starter blankman via AccessMonster.com
  • Start date Start date
B

blankman via AccessMonster.com

Hi, does anybody know how to delete a table in vba if it exists. See what I
did is, I wrote a funtion that returns a boolean value if it exists or not
and when I'm deleting a table I try to run in the sub procedure but I'm
getting no where.
Here's what I got so far.
1 This funtion returns a boolean value for exitences of table.

Public Function TableExists(TableName As String) As Boolean
' Check to see if Table (TableName) exists and return answer
Dim db As Database ' Any Database
Dim td As TableDef ' Any Table Definition
Set db = CurrentDb ' Get current database
For Each td In db.TableDefs ' Find table
If td.Name = TableName Then ' If table found
TableExists = True ' Signal caller that table
Exit Function ' Back to caller
End If ' If table found
Next td ' Check next table
Set db = Nothing
End Function

2. Then I delete the table if tableexist returns true.

Public Sub DeleteTable(TableName As String)
Dim TableExists As Boolean
Dim db As Database

If TableExists = True Then
DoCmd.DeleteObject acTable, "tbldoor"
End If

End Sub
 
If you always want to delete the table if it exists, why not just always
delete the table and trap for the "warning" and/or error:

On Error Resume Next
DoCmd.SetWarnings False
DoCmd.DeleteObject acTable, "tbldoor"
Err.Clear
DoCmd.SetWarnings True
 
You are not passing the name of a table

Public Sub DeleteTable(TableName As String)
Dim TableExists As Boolean
Dim db As Database

If TableExists(TableName)Then
DoCmd.DeleteObject acTable, TableName
End If
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top