Hi
I have a small single user database that I want the user to be able backup periodically. I've a command button on the main form and have found the code below on here which is almost perfect for what I want. The only problem is the database I have is located on the c: drive and I want it backup to a USB pen drive (K: drive). The code below backs up to the same location. I'm a bit of novice so not sure how to do it. Can anyone help?
Private Sub Backup_Button_Click()
Dim str As String
Dim buf As String
Dim MD_Date As Variant
Dim fs As Object
Dim source As String
Const conPATH_FILE_ACCESS_ERROR = 75
On Error GoTo Backup_Button_Backup
'buf = Back Up Folder
'buf is created if it does not exist
'CurrentProject.Path = the path that the FE is located
buf = CurrentProject.Path & "\Backups\"
MkDir buf
Resume Backup_Button_Backup
Backup_Button_Backup:
'Use dd-mm-yyyy hh-mm-ss as folder name. Change as needed.
MD_Date = Format(Date, "dd-mm-yyyy ") & Format(Time, "hh-mm-ss")
str = CurrentProject.Path & "\Backups\" & MD_Date
'Source = where the data is stored
source = CurrentProject.Path
MkDir str
Set fs = CreateObject("Scripting.FileSystemObject")
'Change the file extension as needed
fs.CopyFile source & "*.mdb", str
Set fs = Nothing
MsgBox "Data backup at " & vbCrLf & MD_Date & vbCrLf & "successful!", _
vbInformation, "Backup Successful"
Exit_Button_Backup:
Exit Sub
Err_Button_Backup:
If Err.Number = conPATH_FILE_ACCESS_ERROR Then
MsgBox "The following Path, " & str & ", already exists or there was an Error " & _
"accessing it!", vbExclamation, "Path/File Access Error"
Else
MsgBox Err.Description, vbExclamation, "Error Creating " & str
End If
Resume Exit_Button_Backup
End Sub
I have a small single user database that I want the user to be able backup periodically. I've a command button on the main form and have found the code below on here which is almost perfect for what I want. The only problem is the database I have is located on the c: drive and I want it backup to a USB pen drive (K: drive). The code below backs up to the same location. I'm a bit of novice so not sure how to do it. Can anyone help?
Private Sub Backup_Button_Click()
Dim str As String
Dim buf As String
Dim MD_Date As Variant
Dim fs As Object
Dim source As String
Const conPATH_FILE_ACCESS_ERROR = 75
On Error GoTo Backup_Button_Backup
'buf = Back Up Folder
'buf is created if it does not exist
'CurrentProject.Path = the path that the FE is located
buf = CurrentProject.Path & "\Backups\"
MkDir buf
Resume Backup_Button_Backup
Backup_Button_Backup:
'Use dd-mm-yyyy hh-mm-ss as folder name. Change as needed.
MD_Date = Format(Date, "dd-mm-yyyy ") & Format(Time, "hh-mm-ss")
str = CurrentProject.Path & "\Backups\" & MD_Date
'Source = where the data is stored
source = CurrentProject.Path
MkDir str
Set fs = CreateObject("Scripting.FileSystemObject")
'Change the file extension as needed
fs.CopyFile source & "*.mdb", str
Set fs = Nothing
MsgBox "Data backup at " & vbCrLf & MD_Date & vbCrLf & "successful!", _
vbInformation, "Backup Successful"
Exit_Button_Backup:
Exit Sub
Err_Button_Backup:
If Err.Number = conPATH_FILE_ACCESS_ERROR Then
MsgBox "The following Path, " & str & ", already exists or there was an Error " & _
"accessing it!", vbExclamation, "Path/File Access Error"
Else
MsgBox Err.Description, vbExclamation, "Error Creating " & str
End If
Resume Exit_Button_Backup
End Sub