Creating Application Backup

L

LJG

Hi Guys,

Anyone know of a routine to create a backup of your database at the click of
a Button?

Thanks
Les
 
D

Douglas J. Steele

If your application is split into a front end (containing the queries,
forms, reports, macros and modules) linked to a back end (containing just
the tables), you can use the CompactDatabase method to create a backup of
your back end, provided nobody's using the tables at the time.

If you haven't split your application, you should.
 
L

LJG

Hi Douglas,

Yes I have it split as backend and forms, and have it set to compact on
close. This however, does not create a backup copy, and wonder if this is
possible to say create a backup copy of the database.

Thanks
 
R

Rick Brandt

LJG said:
Hi Douglas,

Yes I have it split as backend and forms, and have it set to compact
on close. This however, does not create a backup copy, and wonder if
this is possible to say create a backup copy of the database.

Actually in a split design compact-on-close only compacts the front end file so
your data file is not even getting compacted currently.

I believe what Douglas was suggesting was that you have code in your front end
that compacts your back end using...

dbEngine.CompactDatabase "Source Path", "Target Path"

As long as no users are actively using any of the objects in the back end you
can use the above to compact to a different file name (you cannot compact the
file onto itself as you can in the GUI) which could be your backup and then you
could copy that file over the original back end so that it ends up compacted.
 
R

Rosco

Les,
The following code is form A2K Help :

Public Sub CompactAndEncrypt()

Dim je As New JRO.JetEngine

' Make sure that a file doesn't exist with the name of
' the compacted database.
If Dir("C:\Program Files\Microsoft Office\" & _
"Office\Samples\Northwind2.mdb") <> "" Then Kill _
"C:\Program Files\Microsoft
Office\Office\Samples\Northwind2.mdb"

' Compacts and encrypts version Northwind database.
je.CompactDatabase _
"Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=C:\Program Files\Microsoft Office\" & _
"Office\Samples\Northwind.mdb", _
"Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=C:\Program Files\Microsoft Office\" & _
"Office\Samples\Northwind2.mdb;" & _
"Jet OLEDB:Encrypt Database=True"

End Sub

Look up CompactDatabase in the VBA help for more details. As you can
see you can compact your DB to a different file name and in a
different location. Note the last line in the code encrypts the back
up. Set to false if you do not want to encrypt.

You will probably want to modify it to suit your purposes. Just call
the routine from a command button.

I save my back ups with the ".bak extention. When I want to restore a
file, I just change the extention back to mdb or mde and I'm good to
go.

Hope this helps
Rosco
 
L

LJG

Like that simple code, but how can I get it to suffix current date&time to
db name to create a unique backup each time.

Thanks
Les
 
D

Douglas J. Steele

Try something like:

Dim strBackupFile As String
Dim strCurrentFile As String
Dim strCurrentLockFile As String

strCurrentFile = "C:\MyBackendDatabase.mdb"
strCurrentLockFile = "C:\MyBackendDatabase.ldb"
strBackupFile = "C:\MyBackendDatabase_" & _
Format$(Now(), "yyyy-mm-dd hh.nn") & ".mdb

If Len(Dir(strCurrentLockFile)) > 0 Then
MsgBox "Cannot compact the database: it's in use"
Else
If Len(Dir(strBackupFile)) > 0 Then
Kill strBackupFile
End If
dbEngine.CompactDatabase strCurrentFile, strCurrentBackupFile
End If
 
L

LJG

Hi Rosco,

Thanks for that code. Have tried it and it works fine if the backend is not
already open.However, if I put this on my form the DB will be open always.

Thanks
Les
 
T

Todd Shillam

Just answered a similiar question--I usually use a batch script (it will copy the file even if its open). However, I also have a Microsoft reference that discusses this topic as well.

Here's the Microsoft reference:

http://support.microsoft.com/default.aspx?scid=kb;en-us;207703

Otherwise....

You can call a batch script using a command button that would copy your file. A batch file is coded using the DOS language.


STEP 1) Copy the following, open Notepad, and paste the code inside. Save the file as Backup.bat and be sure to save it in the same folder as your application.

======== Copy Here =========
@@echo off

echo.
echo ===============
echo STARTING BACKUP
echo ===============
echo Please standby...
ping localhost -n 3 > nul

::Edit the Location for your File and Where to Backup
xcopy /q/y/c/e "C:\Program Files\ApplicationName\ApplicationFile.mdb" "E:\Backups\*.*" > nul

cls
echo.
echo Backup Complete! Standby...
ping localhost -n 3 > nul

======== End Here =========

STEP 2) Copy the following code and paste it into a button's OnClick event:


'=========================================
'DATE:
'AUTHOR:
'COMMENTS:
'
'1) This subroutine launches a MS-DOS
'batch script to perform a backup of
' the application.
'=========================================


'DECLARING VARIABLE
Dim strAppName As String

'INITIALIZING VARIABLE (EDIT THE LOCATION OF THE BATCH FILE IF NEEDED)
strAppName = "C:\Program Files\ApplicationName\Backup.bat"

'CALLING MS-DOS BATCH SCRIPT TO PERFORM APPLICATION UPDATE
Call Shell(strAppName, 1)

STEP 3) Test the button and make sure it works!

Best regards,

Todd
Hi Rosco,

Thanks for that code. Have tried it and it works fine if the backend is not
already open.However, if I put this on my form the DB will be open always.

Thanks
Les
 
R

Rick Brandt

LJG said:
Hi Rosco,

Thanks for that code. Have tried it and it works fine if the backend
is not already open.However, if I put this on my form the DB will be
open always.

So use a menu item or a form that is not bound to the back end. You cannot do
this stuff while the file is open.
 
Top