Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 213
  • Last Modified:

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

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
pwdells
Asked:
pwdells
  • 4
  • 2
1 Solution
 
Jeffrey CoachmanMIS LiasonCommented:
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
 
Jeffrey CoachmanMIS LiasonCommented:
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
 
pwdellsAuthor Commented:
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
Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

 
Jeffrey CoachmanMIS LiasonCommented:
(...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
 
pwdellsAuthor Commented:
Sorry that it took so long to rate.  Your code was right on.

Thank you!
0
 
Jeffrey CoachmanMIS LiasonCommented:
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now