Public Function BackUpAccess()
Dim sourceFile As String, destinationFile As String
Dim aFSO As Variant
Dim path As String, name As String
sourceFile = CurrentProject.FullName
path = CurrentProject.path
name = CurrentProject.name
destinationFile = path & "\LatestBackups\" & Left(name, Len(name) - 6) & "_backup" & "_" & _
Year(Now) & "_" & Month(Now) & "_" & Day(Now) & ".accdb"
'this removes a file created on the same day
If Dir(destinationFile) <> "" Then
Kill destinationFile
End If
'this creates a backup into destination path
If Dir(destinationFile) = "" Then
Set aFSO = CreateObject("Scripting.FileSystemObject")
aFSO.CopyFile sourceFile, destinationFile, True
MsgBox "A database backup has been stored under " & destinationFile
End If
End Function
Call BackUpAccess()
I saw this somewhere, recently, and archived it. I haven't tried it myself, but the OP of the post reported that it worked for him without any problem.
Create a SubFolder to the Folder that holds your Access File, naming it LatestBackups. Next, enter this code into a Standard Module.
Save the Module, naming it modBackUp.Code:Public Function BackUpAccess() Dim sourceFile As String, destinationFile As String Dim aFSO As Variant Dim path As String, name As String sourceFile = CurrentProject.FullName path = CurrentProject.path name = CurrentProject.name destinationFile = path & "\LatestBackups\" & Left(name, Len(name) - 6) & "_backup" & "_" & _ Year(Now) & "_" & Month(Now) & "_" & Day(Now) & ".accdb" 'this removes a file created on the same day If Dir(destinationFile) <> "" Then Kill destinationFile End If 'this creates a backup into destination path If Dir(destinationFile) = "" Then Set aFSO = CreateObject("Scripting.FileSystemObject") aFSO.CopyFile sourceFile, destinationFile, True MsgBox "A database backup has been stored under " & destinationFile End If End Function
In an appropriate event, enter
Code:Call BackUpAccess()
Linq ;0)>
Public Function BackUpAccess()
Dim sourceFile As String, destinationFile As String
Dim aFSO As Variant
Dim sourcePath As String, sourceName As String
Dim destPath As String, destName As String
sourceFile = CurrentProject.FullName
sourcePath = CurrentProject.path
sourceName = CurrentProject.name
destPath = sourcePath & "\backup"
' make backup directory if not exists
If Dir(destPath) = "" Then
MkDir (destPath)
End If
destinationFile = destPath & "\" & Left(sourceName, Len(sourceName) - 6) & "_backup" & "_" & Format(Now(), "yyyymmdd") & ".accdb"
'this removes a file created on the same day
If Dir(destinationFile) <> "" Then
Kill destinationFile
End If
'this creates a backup into destination path
If Dir(destinationFile) = "" Then
Set aFSO = CreateObject("Scripting.FileSystemObject")
aFSO.CopyFile sourceFile, destinationFile, True
'MsgBox "A database backup has been stored under " & destinationFile
End If
End Function
the directory does exist, i was expecting the code to skip the mkdir if it evaluated falseHi Bryan,
Try to do a Debug.Print destPath, so you can see what folder path you're trying to create. If it's a full path, I am not sure MKDIR can do that, but I could be wrong though. Cheers!
PS. I just did a quick test. MKDIR cannot make a subfolder if the parent folder does not exist. You'll have to create one folder at a time, it seems.
Again, did you do a debug.print to verify what the computer was seeing versus what your eyes are seeing? Just curious...the directory does exist, i was expecting the code to skip the mkdir if it evaluated false
debug.print shows C:\Users\bryan\Documents\msAccess\beacon\backup; so why does the code not skip the mkdir?Again, did you do a debug.print to verify what the computer was seeing versus what your eyes are seeing? Just curious...
If that folder exists, then that's a good question. Can you change the debug.print to the following and post a screenshot of the result, please? Thanks.debug.print shows C:\Users\bryan\Documents\msAccess\beacon\backup; so why does the code not skip the mkdir?
MsgBox "[" & destPath & "]"
If that folder exists, then that's a good question. Can you change the debug.print to the following and post a screenshot of the result, please? Thanks.
Code:MsgBox "[" & destPath & "]"
Okay, it's starting to get interesting. Could you try the following now? Thanks.
MsgBox Dir(destPath)
MsgBox Dir(destPath, 16)
Hi Bryan. Did you see my last post? Just curious...
MsgBox Dir(destPath) came up empty, whats that tell me?Okay, it's starting to get interesting. Could you try the following now? Thanks.
After that, try it this way.Code:MsgBox Dir(destPath)
Code:MsgBox Dir(destPath, 16)
That tells me that maybe you're not looking at a folder.MsgBox Dir(destPath) came up empty, whats that tell me?
So, if the new code I provided returns that, then perhaps changing your code should make it work now?
If Dir(detPath, 16) = "" Then
that did it! thanks for your helpThat tells me that maybe you're not looking at a folder.
So, if the new code I provided returns that, then perhaps changing your code should make it work now?
Code:If Dir(detPath, 16) = "" Then
Glad to hear you got it sorted out. Good luck with your project.that did it! thanks for your help