Back To Top


MS Access VBA Delete Table For versions 2000, 2002, 2003, 2007

Go here To Delete Records in the Query Builder.
Go here To Delete All Tables in VBA.
Go here To Delete a Field in VBA.
Go here To Delete Records in VBA.

 

The VBA DROP Table Sample is available for download here in a zipped MS Access Database. The below picture (click to enlarge) shows the form.

MS Access VBA Delete Table Button 

The Actual VBA Code to Delete a Table using criteria for the Drop Query, you should be able to copy/paste right into a module:

Function DeleteTable( ByVal TableName As String ) As Boolean

  
'References: Microsoft Access 11.0 Object Library, Microsoft DAO 3.6 Object Library
   'Set references by Clicking Tools and Then References in the Code View window
   ' Deletes a Table with a given TableName that exists in a database
   ' Accepts
   ' TableName: Name of table
   ' Returns True on success, false otherwise
   'USAGE: DeleteTable "TABLENAME"


  
On Error GoTo errhandler

  
Dim Db As DAO.Database
  
Dim strSql As String

   Set
Db = CurrentDb()

  
'Create the Delte SQL Code from our Three Strings
  
strSql = "DROP TABLE " & TableName

  
If ifTableExists(TableName) = True Then

     
'Print the SQL so we can paste into the query builder if there are errors
     
Debug.Print strSql

     
'delete table if found
     
Db.Execute strSql
  
End If

  
'If no errors return true
  
DeleteTable = True

ExitHere:

  
Set Db = Nothing
  
'Notify the user the process is complete.
  
MsgBox "Delete Table Complete"
  
Exit Function

errhandler:
  
'There is an error return false
  
DeleteTable = False

   With Err
     
MsgBox "Error " & .Number & vbCrLf & .Description, _
            vbOKOnly
Or vbCritical, "DeleteTable"
  
End With

   Resume
ExitHere

End Function

For the above VBA Delete Function in an usable Form with Delete Button, download the Sample database in ZIP format. The sample includes 2 tables in which to test on.

You may post this tutorial on your website or in a forum. If you do please maintain a Link to Eraseve AP.



For further MS Access tutorials go here: MS Access tutorials

Go here To Check if Table exists in VBA.
Go here To Check if Field exists in VBA.

 

Did you find what you were looking for?
What would you suggest?
mail@eraserve.com
Your Name:
Your Message:
Your Email:

    

 

Custom Search