Link to home
Start Free TrialLog in
Avatar of Giulio Benvenuti
Giulio BenvenutiFlag 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?
Avatar of Daniel Pineault
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.
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
Avatar of Daniel Pineault
Daniel Pineault

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial