I am a newbie. I have use the above backup code to backup my access project while I was developing the code. All worked well. Now that I have split my project into a backend I am trying to backup the backend.
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_be.accbd"
strBackupFile = "BackupDB-" & Format(Date, "yyyy-mm-dd") _
& "_" & Format(Time, "hh_mm") & "-" & strSourceFile ' This Works but time is wrong.
' strBackupFile = "BackupDB-" & Format(Date, "yyyy-mm-dd") _
& "_" & Format(Now, "yyyy-mm-dd hhnnss") & "-" & 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
Dim SQL As String
SQL = "INSERT INTO tblBackupDetails " _
& "(BackupDate, ComputerName, BackupFolder, Filename) " _
& "VALUES ('" & Date & "', '" & Environ("COMPUTERNAME") _
& "', '" & BACKUP_PATH & "', '" & strBackupFile & "');"
DoCmd.RunSQL SQL
SQL = "DELETE * FROM tblBackupDetails WHERE BackupDate < date() - 30;"
DoCmd.RunSQL SQL
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
At the fso line of code I get a File not found error.
My access project in not on a network. It is on a single PC.