windows 10 Office 2021
I have my backup subroutine working. The problem is I need to automatically delete copies of the backups older than 30 days because the system backup has already backed up all the data as part of the system backup. The error is near the bottom of the code. See screen capture picture below

Code:
Option Compare Database
Option Explicit
' This function copies the Access database to a backup
' location when the database is opened.
' It does this only once in any given day.
' Author: Mike Perris - mikeperris.com
' Date: 01-Jun-2012
' Version: 1.2
Public Function BackupOnOpen()
' *** CHANGE THE FOLLOWING LINE TO MATCH YOUR BACKUP DESTINATION
' Ensure you have a \ on the end of the pathname.
Const BACKUP_PATH = "F:\NDC Documents\NDC Access\AutoBackup\"
On Error GoTo BackupOnOpen_Err
' If DCount("BackupDate", "tblBackupDetails", "BackupDate = date()") <> 0 Then
' Exit Function
' End If
Dim strSourcePath As String
Dim strSourceFile As String
Dim strBackupFile As String
strSourcePath = GetFileName(CurrentDb.Name, False) ' false means we want pathname
strSourceFile = GetFileName(CurrentDb.Name, True) ' true means we want filename
strSourcePath = "F:\NDC Documents\NDC Access\"
'strSourceFile = "Beneficiaries.accdb"
strBackupFile = "Backup_ALL " & Format(Date, "yyyy-mm-dd") _
& " @ " & Format(Now, "hh-nn") & "-" & strSourceFile ' This Works but time is wrong. Can not get AM/PM to work
'strBackupFile = "BackupDB-" & Format(Date, "yyyy-mm-dd") _
& "_" & Format(Now, "yyyy-mm-dd hh:nn") & " - " & strSourceFile
'strBackupFile = "BackupDB-" & "_" & Format(Now, "yyyy-mm-dd hhnn") & " - " & strSourceFile
' Format(Now, "yyyy-mm-dd hh\:nn\:ss")
'MsgBox strSourcePath & strSourceFile 'Returns correct value
'MsgBox BACKUP_PATH & strBackupFile 'Returns correct value
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject") 'File not found error
fso.CopyFile strSourcePath & strSourceFile, BACKUP_PATH & strBackupFile, True
Set fso = Nothing
DoCmd.SetWarnings False
'Log BackUp details. Do not need any more
'Dim SQL As String
'SQL = "INSERT INTO tblBackupDetails " _
' & "(BackupDate, ComputerName, BackupFolder, Filename) " _
' & "VALUES ('" & Date & "', '" & Environ("COMPUTERNAME") _
'& "', '" & BACKUP_PATH & "', '" & strBackupFile & "');"
'Remove Older log enteries
'DoCmd.RunSQL SQL
'SQL = "DELETE * FROM tblBackupDetails WHERE BackupDate < date() - 30;"
'DoCmd.RunSQL SQL
'Remove Older BackUp files enteries
' Dim SQL As String
' DoCmd.RunSQL SQL
' SQL = "DELETE * FROM BACKUP_PATH WHERE BackupDate < date() - 30;"
' DoCmd.RunSQL SQL
'MsgBox "Removed Old Backup.", , "Removed Old Files OK"
'Remove Older BackUp files enteries. SECOND TRY
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject") 'File not found error
fso.deletefile strSourcePath & strSourceFile, BACKUP_PATH & strBackupFile WHERE BackupDate < date() - 30; True
Set fso = Nothing
DoCmd.SetWarnings False
MsgBox "All DB Backed UP.", , "Back Up OK"
DoCmd.SetWarnings True
BackupOnOpen_Exit:
Exit Function
BackupOnOpen_Err:
MsgBox Err.Description, , "BackupOnOpen()"
Resume BackupOnOpen_Exit
End Function
' This function is given a filename complete with path, and
' a boolean value.
' If the boolean is true, this function returns only the
' filename (without the path).
' If the boolean is false, this function returns only the
' path (without the filename).
' Author: Mike Perris - mikeperris.com
' Date: 13-Mar-2011
' Version: 1.1
' Revision History:
' Rev Date(yyyy/mm/dd) Description
' 1.1 13/3/2011 Added the boolean bit and extended code to
' extract path as well as filename.
Function GetFileName(FullPath As String, IsFile As Boolean)
Dim icount As Integer
icount = Len(FullPath)
Do Until Mid(FullPath, icount, 1) = "\"
icount = icount - 1
Loop
If IsFile Then
GetFileName = Right(FullPath, Len(FullPath) - icount)
Else
GetFileName = Left(FullPath, icount)
End If
End Function