Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2011-03-20
6
Medium Priority
?
210 Views
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!

Wendee
0
Comment
Question by:pwdells
[X]
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
6 Comments
 
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*'")
rst.MoveFirst
Do While Not rst.EOF
    DoCmd.DeleteObject acTable, rst!Name
    rst.MoveNext
Loop

rst.Close
Set rst = Nothing
0
 
LVL 74

Accepted Solution

by:
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.

    Application.RefreshDatabaseWindow


JeffCoachman
0
 

Author Comment

by:pwdells
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.
0
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!

 
LVL 74

Expert Comment

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



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

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

And:

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.

;-)

Jeff
0
 

Author Closing Comment

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

Thank you!
0
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 35222135
OK,

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
0

Featured Post

Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

Question has a verified solution.

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

In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

670 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