How to determine if table exists in VBA

T

Tony Girgenti

Is there a way to determine if a table exists in the database using VBA ?

I want to be able to delete a table once determined it exists.

Thanks,
Tony
 
M

Michel Walsh

Hi,



CurrentDb.Execute "DROP TABLE tableName"


The table may be not "deletable" (because of some DRI), if so, it would
still be there, but otherwise, just delete it, even if it is not "there"...


Public Function IsTableExist(ByVal tableName As String) As Boolean
Dim db As Database : Set db=CurrentDb
Dim t As TableDef

For each t in db.Tabledefs

If tableName = t.Name then
IsTableExist=true
Exit Function

End If
next t

End Function




Hoping it may help,
Vanderghast, Access MVP
 
P

Paul Overway

The easiest way to do this would be to just delete the table and ignore the
error generated if the table doesn't exist, i.e.,

Dim db As Database

On Error GoTo err_Proc:

Set db = CurrentDb()

db.TableDefs.Delete "sometable"

Exit Sub

err_Proc:

Select Case Err.Number
Case 3265 'Table
Resume Next
Case Else
MsgBox Err.Description, vbExclamation, Err.Source
End Select
 

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

Top