Public Sub BackEndBackUps()
Dim fld As Folder, fil As File
Dim dteMax As Date, flg As Boolean
Dim objFiles As Object, lngFileCount As Long
Dim FolderPth As String
Dim fso As New FileSystemObject
FolderPth = CurrentProject.Path & "\DataBackups" 'path to backup folder
'If the backup folder doesn't exist, create it
If Len(Dir(FolderPth, vbDirectory)) = 0 Then
MkDir FolderPth
End If
dteMax = #1/1/2000#
Set fld = fso.GetFolder(FolderPth)
'get the most recent created date of all the backups in the folder
For Each fil In fld.Files
If dteMax < fil.DateCreated Then
dteMax = fil.DateCreated
End If
Next
Set objFiles = fso.GetFolder(FolderPth).Files
lngFileCount = objFiles.Count
'check if latest backup is older than a certain number of days
For Each fil In fld.Files
If DateDiff("d", dteMax, Date) > 6 Then 'you can change the numeric value to the number of days between backups
flg = True
End If
Next
If flg = True Or lngFileCount = 0 Then
BackUpAndCompactBE FolderPth 'Call to the Back up procedure
End If
'if backup file is older than a certain number of days, delete it
For Each fil In fld.Files
If DateDiff("d", fil.DateCreated, Date) > 28 Then 'you can change the numeric value to the number of days before deleted
fil.Delete
End If
Next
End Sub