Delete table using Where clause?

M

Martin

I would like to write some code that will delete a table if the table name
contains the text "import errors". This is the code I have so far but it
does not work. I think the problem is with the Docmd.Delete line. Can
anyone help me fix this?

Public Function Delete()
Dim db As Database, FileName As String, tbl As TableDef

Set db = CurrentDb()
FileName = "ImportErrors"

For Each tbl In db.TableDefs
If tbl.Name Like FileName Then
MsgBox "Hello"
DoCmd.DeleteObject acTable, FileName
End If
Next

End Function

Thank you,

Martin
 
J

John Spencer

Public Function Delete()
Dim db As Database, FileName As String, tbl As TableDef

Set db = CurrentDb()
FileName = "ImportErrors"

For Each tbl In db.TableDefs
If Instr(1,tbl.Name,FileName) > 0 Then
MsgBox "Hello"
DoCmd.DeleteObject acTable, tbl.Name '<<<<
End If
Next

End Function

This function could fail to delete a table if two tables in the collection
were immediately adjacent and they both contained "Import Errors"
 
D

Douglas J Steele

This function could fail to delete a table if two tables in the collection
were immediately adjacent and they both contained "Import Errors"

To avoid that, you could use:

Public Function Delete()
Dim db As Database, FileName As String, tbl As TableDef
Dim intLoop As Integer

Set db = CurrentDb()
FileName = "ImportErrors"

For intLoop = (db.TableDefs.Count - 1) To 0 Step -1
Set tbl = db.TableDefs(intLoop)
If Instr(1,tbl.Name,FileName) > 0 Then
MsgBox "Hello"
DoCmd.DeleteObject acTable, tbl.Name
End If
Next intLoop

End Function
 
J

John Spencer

Yeah, I knew that. I was just too lazy to rewrite the entire thing.
Thanks for the help.
 

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