PC Review


Reply
Thread Tools Rate Thread

Delete Tables

 
 
alex
Guest
Posts: n/a
 
      11th Jan 2010
Delete Tables

In VBA, I’d like to delete a group of tables (both static and linked)
when the db closes…

I have this function:

Function DeleteTables() 'when mde closes, delete all tables with
suffix of 'Delete'

'If Right(CurrentDb.Name, 3) = "MDE" Then 'if current file is an mde
then

Dim db As Database
Dim tdf As TableDef
Dim lngCnt As Long

Set db = CurrentDb
For lngCnt = db.TableDefs.Count - 1 To 0 Step -1
Set tdf = db.TableDefs(lngCnt)
If Right(tdf.Name, 6) = "Delete" Then 'delete any table with
'Delete' as suffix
db.TableDefs.Delete tdf.Name
'Debug.Print (tdf.Name)
End If
Next lngCnt

Set tdf = Nothing
Set db = Nothing

'End If

End Function

I then call the function (Call DeleteTables) from the close event of a
form that remains open in my db (until the entire db is closed)

It appears that sometimes the code works, and sometimes it does not;
i.e., some tables are deleted while others are not. Also, sometimes
the tables appear to be present, but once you click the object, it
disappears. Can anyone suggest what I may be doing wrong?

Thanks,
alex
 
Reply With Quote
 
 
 
 
Dorian
Guest
Posts: n/a
 
      11th Jan 2010
You could just delete the contents and leave the tables there for next time.
They wont take up much space if empty (you'll need to set 'compact on close'
option).
CurrentProject.Connection.Execute "DELETE * FROM mytablename"
-- Dorian
"Give someone a fish and they eat for a day; teach someone to fish and they
eat for a lifetime".


"alex" wrote:

> Delete Tables
>
> In VBA, I’d like to delete a group of tables (both static and linked)
> when the db closes…
>
> I have this function:
>
> Function DeleteTables() 'when mde closes, delete all tables with
> suffix of 'Delete'
>
> 'If Right(CurrentDb.Name, 3) = "MDE" Then 'if current file is an mde
> then
>
> Dim db As Database
> Dim tdf As TableDef
> Dim lngCnt As Long
>
> Set db = CurrentDb
> For lngCnt = db.TableDefs.Count - 1 To 0 Step -1
> Set tdf = db.TableDefs(lngCnt)
> If Right(tdf.Name, 6) = "Delete" Then 'delete any table with
> 'Delete' as suffix
> db.TableDefs.Delete tdf.Name
> 'Debug.Print (tdf.Name)
> End If
> Next lngCnt
>
> Set tdf = Nothing
> Set db = Nothing
>
> 'End If
>
> End Function
>
> I then call the function (Call DeleteTables) from the close event of a
> form that remains open in my db (until the entire db is closed)
>
> It appears that sometimes the code works, and sometimes it does not;
> i.e., some tables are deleted while others are not. Also, sometimes
> the tables appear to be present, but once you click the object, it
> disappears. Can anyone suggest what I may be doing wrong?
>
> Thanks,
> alex
> .
>

 
Reply With Quote
 
alex
Guest
Posts: n/a
 
      12th Jan 2010
On Jan 11, 5:29*pm, Dorian <Dor...@discussions.microsoft.com> wrote:
> You could just delete the contents and leave the tables there for next time.
> They wont take up much space if empty (you'll need to set 'compact on close'
> option).
> CurrentProject.Connection.Execute "DELETE * FROM mytablename"
> -- Dorian
> "Give someone a fish and they eat for a day; teach someone to fish and they
> eat for a lifetime".
>
>
>
> "alex" wrote:
> > Delete Tables

>
> > In VBA, I’d like to delete a group of tables (both static and linked)
> > when the db closes…

>
> > I have this function:

>
> > Function DeleteTables() 'when mde closes, delete all tables with
> > suffix of 'Delete'

>
> > 'If Right(CurrentDb.Name, 3) = "MDE" Then 'if current file is an mde
> > then

>
> > Dim db As Database
> > Dim tdf As TableDef
> > Dim lngCnt As Long

>
> > * * Set db = CurrentDb
> > * * For lngCnt = db.TableDefs.Count - 1 To 0 Step -1
> > * * * * Set tdf = db.TableDefs(lngCnt)
> > * * * * If Right(tdf.Name, 6) = "Delete" Then 'delete any table with
> > 'Delete' as suffix
> > * * * * * * db.TableDefs.Delete tdf.Name
> > * * * * * * 'Debug.Print (tdf.Name)
> > * * * * End If
> > * * Next lngCnt

>
> > * * Set tdf = Nothing
> > * * Set db = Nothing

>
> > 'End If

>
> > End Function

>
> > I then call the function (Call DeleteTables) from the close event of a
> > form that remains open in my db (until the entire db is closed)

>
> > It appears that sometimes the code works, and sometimes it does not;
> > i.e., some tables are deleted while others are not. *Also, sometimes
> > the tables appear to be present, but once you click the object, it
> > disappears. *Can anyone suggest what I may be doing wrong?

>
> > Thanks,
> > alex
> > .- Hide quoted text -

>
> - Show quoted text -


Hi Dorian,
I think I found my problem; my code was trying to execute before all
of the forms had released the applicable tables as data sources.
alex
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Delete Query - Delete matching records from 2 tables - Access 2000 Chris Stammers Microsoft Access Queries 4 22nd Jan 2009 02:45 PM
Delete Query Error "Can't delete from specified tables Maureen227 Microsoft Access Queries 4 26th Jul 2006 04:58 PM
Delete Query Error "Can't delete from specified tables Maureen227 Microsoft Access 6 26th Jul 2006 03:36 PM
Delete query (joined to 2 tables) won't delete Please help--dim bu =?Utf-8?B?UGF1bHltb24=?= Microsoft Access Queries 3 12th May 2005 12:27 PM
How to delete all records from tables in VBA wihout getting the confirm delete prompt ? Adrian Microsoft Access 4 16th Aug 2004 02:23 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 02:08 PM.