Avatar of Giulio Benvenuti
Giulio Benvenuti
Flag for Italy asked on

Delete all tables except last day

Function delTBackupDatabase()
 Dim db As DAO.Database
 Dim tdf As DAO.TableDef
 Set db = CurrentDb
 For Each tdf In db.TableDefs
    If ("TBackupDatabase " & Format(Date, "dd/mm/yyyy")) < (Date - 3) Then
       DoCmd.DeleteObject acTable, tdf.Name
    End If
 Next

Access.RefreshDatabaseWindow
End Function

Open in new window

Trying to delete all tables after 3 days. It doesnt work. What's wrong?
What about to create a new table to make it once every day?
VBA* Access

Avatar of undefined
Last Comment
Daniel Pineault

8/22/2022 - Mon
Daniel Pineault

Normally you don't create and delete tables.  This leads to bloating and other issues and make us question your data structure and normalization.

Normally, you delete records, or archive them.  Add a Date\Time field to track the entries so you can sort/filter/...

It doesnt work 
What happens exactly?

If the table is involved in relationships, then you won't be able to delete it until you delete the relationships first.
Daniel Pineault

Also

If ("TBackupDatabase " & Format(Date, "dd/mm/yyyy")) < (Date - 3) Then

Open in new window

will never work.you're comparing a string against a date?
ASKER CERTIFIED SOLUTION
Daniel Pineault

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck