Back-Up

scouser

Registered User.
Local time
Today, 15:40
Joined
Nov 25, 2003
Messages
767
I am getting an error when I attempt to back-up my DB
Code:
Dim fso As FileSystemObject

Dim sSourcePath As String
Dim sSourceFile As String
Dim sBackupPath As String
Dim sBackupFile As String

sSourcePath = "C:\Documents and Settings\My Name\My Documents\Access\MyDatabase\DB UK\"
sSourceFile = "DB UK.mdb"
sBackupPath = "C:\Database\Backups\"
sBackupFile = "BackupDB_" & Format(Date, "mmddyyyy") & "_" & Format(Time, "hhmmss") & ".mdb"

Set fso = New FileSystemObject
[B]fso.CopyFile sSourcePath & sSourceFile, sBackupPath & sBackupFile, True[/B]
Set fso = Nothing

Dim sWinZip As String
Dim sZipFile As String
Dim sZipFileName As String
Dim sFileToZip As String

sWinZip = "C:\Program Files\WinZip\WINZIP32.EXE" 'Location of the WinZip program
sZipFileName = Left(sBackupFile, InStr(1, sBackupFile, ".", vbTextCompare) - 1) & ".zip"
sZipFile = sBackupPath & sZipFileName
sFileToZip = sBackupPath & sBackupFile

Do I need to install something from 'Tools' 'References' whilst viewing the module in order for it to work?

Used to work?
Cheers,
Phil.
 
1. What is the error message?
2. Are you trying to copy the same DB you are using?
 
I do not know which post you found my backup function in but you need to revisit it and follow the directions for setting the correct reference to use the fso.CopyFile.

And yes, you can copy the current db that is open and calling the backup function. Just ensure that there are no other users in the db to minimize any data corruption during the copy process.
 
Thanks

Many moons ago I started a thread on the above subject after finding the back-up function. Mr H was most helpful back then (thanks once again). I feel I am doing all as I did then....I will check back as suggested and see if there is an obvious oversight on my part!!
:D
 
Here's a code that I use in my application. I changed my source and destination paths to yours. I have a command button on my form that calls the function on the "On Click" event using "=BackUpAccess()".

Module Code:
======================================
Option Compare Database

Public Function BackUpAccess()

' ***************************************************
' Script To Backup an Access MDB file
'
' Danny J. Lesandrini April 6, 2001
' datafast@home.com
' http://datafast.cjb.net
'
' This script was created to simplify the process
' of backing up a development copy of an MS Access
' database.
'
' The same process can be used in reverse to copy
' a development master file to the user's desktop
' for editing. The file is renamed with a prefix
' created from the current Date and Time, thus
' making it unique (assuming you don't execute the
' script more frequently than once per minute).
'
'
'
' ************************************************

Dim strSourcePath
Dim strTargetPath
Dim strFileName

Dim objScript

Dim strPrefix
Dim strMonth
Dim strDay
Dim strYear
Dim strHour
Dim strMinuite

Dim strSource
Dim strTarget
Dim strMsg

Dim strFullDatabaseName
Dim strDatabaseName

' ****
' **** Set your Source and Target paths here.
' ****
strSourcePath = "C:\Documents and Settings\My Name\My Documents\Access\MyDatabase\DB UK\"
strTargetPath = "C:\Database\Backups\"

strFullDatabaseName = GetdbName()
strFileName = Left(strFullDatabaseName, Len(strFullDatabaseName) - 4)

' **** To provide a unique prefix for the backup file, the current
' **** date and time is parsed and concatonated into a string variable
'
strMonth = DatePart("m", Date)
If Len(strMonth) = 1 Then strMonth = "0" & strMonth

strDay = DatePart("d", Date)
If Len(strDay) = 1 Then strDay = "0" & strDay

strYear = Right(DatePart("yyyy", Date), 2)

strHour = DatePart("h", NOW())
If Len(strHour) = 1 Then strHour = "0" & strHour

strMinuite = DatePart("n", NOW())
If Len(strMinuite) = 1 Then strMinuite = "0" & strMinuite

'strPrefix = strMonth & "-" & strDay & "-" & strYear & "-" & strHour & strMinuite & "_"
strDateStamp = "[" & strMonth & "-" & strDay & "-" & strYear & "]"


' **** Now that we know the Root Path, File Name and new Prefix,
' **** the Source and Target paths can be created.
'

strSource = strSourcePath & strFileName & ".mdb"
strTarget = strTargetPath & strFileName & strDateStamp & ".mdb"

' **** Create the Scripting Object and copy the file
'
Set objScript = CreateObject("Scripting.FileSystemObject")

DoCmd.Hourglass True

objScript.CopyFile strSource, strTarget

' **** Clean up
'
Set objScript = Nothing
DoCmd.Hourglass False

' **** Inform User that Operation is Completed
'
strMsg = "The following file:" & vbCrLf & Space(5)
strMsg = strMsg & strFileName & strDateStamp & ".mdb" & vbCrLf & vbCrLf
strMsg = strMsg & "has been Archived."
MsgBox strMsg, , "Finished"

End Function

' **************************************
Public Function GetdbName()
GetdbName = Right(CurrentDb.Name, Len(CurrentDb.Name) - InStrR(CurrentDb.Name, "\", 1))
End Function

================================================
 
scouser said:
Many moons ago I started a thread on the above subject after finding the back-up function. Mr H was most helpful back then (thanks once again). I feel I am doing all as I did then....I will check back as suggested and see if there is an obvious oversight on my part!!
:D
'You must set a reference to the 'Microsoft Scripting Runtime' for the fso.CopyFile piece to work!
 
Working

I got the path working OK.
Had another issue that was down to my brothers winzip being a demo version.
I upgraded this and it works (slight blip). It displays the winzip window briefly prior to backing up the database to the specified location!!
Cheers Guys,
Phil.
 
Questions on this process

Ok! :rolleyes:
I have got the idea of some of how this BackUp Script works. I just a few questions dealing with my existing database. First with my database I have the tables completely separated from the rest of the objects. This is for ease of upgrades. So long as I don't change the underlying table structure all I have to do is copy the database using the same path and file name for the linked data. So how would this script work in this application to copy "data.mdb" to "saved data.mdb"? Second can this script be set so that the program exits, and how would that look? Does the target file need to be existing or can it check to see if it exists and if not create it?
 

Users who are viewing this thread

Back
Top Bottom