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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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.
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

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

Industry Leaders: 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

It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
The Windows Phone Theme Colours is a tight, powerful, and well balanced palette. This tiny Access application makes it a snap to select and pick a value. And it doubles as an intro to implementing WithEvents, one of Access' hidden gems.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…

623 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