Selective deleting in tables with VBA

PB7

Board Regular
Joined
Mar 1, 2011
Messages
58
Hello Access users.

Thought this VBA code module would be simple, but as usual, probably missing a nuance, and this doesn't work.

I need to selectively delete about 5 tables in a 2007 database before the major processing, and I thought I could do so in VBA with following example of code:

For Each tblDef in CurrentDb.TableDefs
If tblDef.Name = "TableThatMustBeDeleted" Then
DoCmd.SelectObject acTable, tblDef.Name, True
DoCmd.DeleteObject acTable, tblDef.Name
End If
Next tblDef

Also have:

Dim db as Object
Set db = CurrentDb

Dim tblDef as TableDef

CAN ANYONE SPOT WHAT I'M DOING WRONG??

Thanks in advance to any help.
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
How are you differentiating the different table names?
If you have the table name hard-coded, won't it delete just that one table?

Is that what the problem is, or is it not deleting anything or are you getting some sort of error?
 
Upvote 0
In case it helps at all, here is a script that I use to delete any ImportErrors tables that are creating from file imports (very similar to yours, except for the IF statment).

Code:
    Dim tblDef As TableDef
'   Delete old import errors tables
    For Each tblDef In CurrentDb.TableDefs
        If InStr(1, tblDef.Name, "ImportErrors") > 0 Then
            DoCmd.SelectObject acTable, tblDef.Name, True
            DoCmd.DeleteObject acTable, tblDef.Name
        End If
    Next tblDef
 
Upvote 0
Joe, right, I am hard-coding the table names. If that snippet of code works to delete "TableThatMustBeDeleted", then I will replicate that snippet 5 times, with each of 5 snippets to refer to a different table.

I sense there are other ways, maybe better ways, to do this deleting, but as a VBA novice, I'm going for whatever I can visualize working right now.

So, my code doesn't ultimately delete the the linked table...its still there when I run this code. To my dismay.

Looking for mercy and survival, as is often the case with VBA. Thanks.
 
Upvote 0
Joe, your code works great, and solves my problem.

I thought a slight variation would work, but it didn't. Thanks..huge help here!!
 
Upvote 0

Forum statistics

Threads
1,224,590
Messages
6,179,756
Members
452,940
Latest member
rootytrip

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top