Skip to content Skip to sidebar Skip to footer

Vba Script To Check If Table Exist On Ms Access, Delete If It Does

I need to implement a check to see if TableA exists, if it does, drop the entire table. If it doesn't, i will create the table. I couldn't really find out if this is possible to im

Solution 1:

There is a much easier way to do this than the code above.

OnErrorResumeNext
  currentdb.execute "DROP TABLE TABLENAME;"OnErrorGoto0

What this does is tries to delete the table and skips the line if an error is generated because the table does not exists. Only 3 lines and runs faster.

Solution 2:

Consider using the TableDefs collection where you iterate through items and conditionally drop or create using same DDL SQL statements passed in code.

Dim db As Database 
Dim tbldef As TableDef

Set db = CurrentDb

Foreach tbldef in db.TableDefs 
   If tbldef.Name = "TableName"Then 
      db.Execute "DROP TABLE " & tbldef.Name, dbFailOnError
   EndifNext tbldef

db.Execute "CREATE TABLE TableName (...rest of SQL...);", dbFailOnError    

' UNINITIALIZE OBJECTS Set tbldef = NothingSet db = Nothing

Post a Comment for "Vba Script To Check If Table Exist On Ms Access, Delete If It Does"