Hi. In one of my applications I have an Access DB which is set via the Windows task scheduler to open at 5am and close itself automatically when done. One of the tasks it performs is to copy a master database to a backup location. It renames the previous backup copy of the master DB then copies in the master DB.
The code uses the filesystem object to do the copying and renaming and is listed below. It refers to four form controls which are bound to fields in an underlying table that record the master url for the database being copied, the copy url for the location it is being copied to, and the file names (with extensions) of the original and copied files.
I provide this as a somewhat rough and ready routine, which for me is in use every day to provide an off-line copy of the master database on which I can work without danger of damaging a live application. In my case the database copied records the details of all incidents attended by the Fire and Rescue Service I work for, so working on the live data is too big a risk.
- Private Sub CopyDB_Click()
-
Dim fso As Object, f2 As Object, f3 As Object
-
Dim strCopyFrom As String, strCopyTo As String, strPrev As String
-
Dim strPrevLoc As String
-
On Error Resume Next
-
Set fso = CreateObject("Scripting.FileSystemObject")
-
strCopyFrom = Me.Control_DB_Original_URL & "\" & Me.Control_DB_Name
-
strCopyTo = Me.Control_DB_Copy_URL & "\" & Me.Copy_DB_Name
-
strPrev = "Prev-" & Me.Copy_DB_Name
-
strPrevLoc = Me.Control_DB_Copy_URL & "\" & strPrev
-
Me.Message = "Copy started at " & Format(Time(), "hh:mm") & " hrs." & vbCrLf & "This can take around 10 mins to complete..."
-
Me.Repaint
-
DoCmd.Hourglass True
-
fso.DeleteFile strPrevLoc ' delete the previous backup version of the file.
-
Set f3 = fso.Getfile(strCopyTo)
-
f3.Name = strPrev
-
On Error GoTo errhandler
-
Set f2 = fso.Getfile(strCopyFrom)
-
f2.Copy (strCopyTo)
-
Me.Message = Me.Message & vbCrLf & "Copy ended at " & Format(Time(), "hh:mm") & " hrs."
-
Me.Repaint
-
Set fso = Nothing
-
DoCmd.Hourglass False
-
DoCmd.Close acForm, Me.Name
-
Exit Sub
-
errhandler:
-
MsgBox "Error: " & Err.Number & " -> " & Err.Description
-
DoCmd.Hourglass False
-
End Sub
-Stewart