Go Premium for a chance to win a PS4. Enter to Win


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

Posted on 2011-03-20
Medium Priority
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 2000 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.
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

916 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