Delete a table if it has a '$' in the title, through VBA.

Posted on 2011-03-20
Last Modified: 2012-06-21
Hello Experts,

I run a mass import every week -- sometimes several times.  However, through the import process, I end up generating many tables with the $Errors listing.  I don't want those tables to exist.  

How do I drop those tables after the completion of the import process?  (Via VBA)

Thank you!

Question by:pwdells
  • 4
  • 2
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 35175573
There are many ways, here is one:

Dim rst As DAO.Recordset
Set rst = CurrentDb.OpenRecordset("Select Name From Msysobjects WHERE Name Like '*error*'")
Do While Not rst.EOF
    DoCmd.DeleteObject acTable, rst!Name

Set rst = Nothing
LVL 74

Accepted Solution

Jeffrey Coachman earned 500 total points
ID: 35175602
You can further refine the criteria to just tables or just tables
    Where Type=1

Or something like this:

Dim tbl As TableDef
    For Each tbl In CurrentDb.TableDefs
        If tbl.Name Like "*ImportError*" Then
            DoCmd.DeleteObject acTable, tbl.Name
        End If
    Next tbl

...Where the focus is just on the name

But I'm sure you get the idea, you can make this as specific as you like.

Also, depending on where this code leaves you, you may have to refresh the DB window.



Author Comment

ID: 35175924
Thank you so much.  I knew it was in the MSysObjects...but I couldn't exactly recall what it was that I used to work with a few year ago.  

Flowers for Algernon Syndrome when I am not working with this code daily.
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 35177999
(...pauses for a moment to Google "Flowers for Algernon Syndrome")

For the record, to answer your Question as Titled, ...
use this as the Where clause:

WHERE Name Like "*$*"
...for the recordset code.


If tbl.Name Like "*$*" Then
...for the table def code...

Just be aware that you may have $ symbols in Excel Imported tables/and or sheets,

so again, you must choose the exact criteria combination that you feel comfortable with.



Author Closing Comment

ID: 35221819
Sorry that it took so long to rate.  Your code was right on.

Thank you!
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 35222135

But to be sure, you should always provide some sort of "Confirmation" code whenever you do anything "destructive" (Delete tables, where there is no ability to undo)

Dim tbl As TableDef
dim bytConfirm as byte
bytConfirm =msgbox ("Delete Error tables?",vbyesNo+vbquestion)
    If bytConfirm =vbno then
        exit sub
    end if

    For Each tbl In CurrentDb.TableDefs
        If tbl.Name Like "*ImportError*" Then
            DoCmd.DeleteObject acTable, tbl.Name
        End If
    Next tbl

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The first two articles in this short series — Using a Criteria Form to Filter Records ( and Building a Custom Filter ( — discuss in some detail how a form can be…
It took me quite some time to sort out all the different properties of combo and list boxes available from Visual Basic at run-time. Not that the documentation is lacking: the help pages are quite thorough and well written. The problem was rather wh…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now