Wednesday, March 26, 2008

How to backup Access file using VBA code and zip it

Hi, it's been a lot of time since last post.
i have found this nice access file that is very useful if you have an access database file that is separated from the original main file, it's mean like this scenario:

banner370x120[1]


you have the main program that is an access file (*.mdb or *.mde) (like main.mde) that contains all the forms and the reports and the modules.. and you have a separate file mdb or mde also (like data.mdb) that contains the tables and the data..
in this way you can't make a backup of the data file using backup database tool located in the access software... you need to backup the data.mdb file...

so how you do this!!!

using the modules located in this file "backup demo" you can easily do this..
just click the button and the program will check if you have installed Winzip... if you did it will backup the file to a temporary folder and zip it than save it in the location you want..
else
it will just copy the file to the location you give it.
end surely it will set the name of the file backup_DATE_TIME.. where date is the current date and the time is the current time..

if you need to change anything you can go deeply in the code..

here is the file links:
http://www.ziddu.com/download/3537240/BackUpDemo.zip.html


here you can view the code of the ZipandBackUpDb


Function ZipandBackUpDb()
On Error GoTo Err_BackUpDb

'this line is very important to handle files
'it's very necessary to add the "Microsoft scripting runtime" reference from the tools->references in the VBA window

Dim fso As FileSystemObject

Dim sSourcePath As String
Dim sSourceFile As String
Dim sBackupPath As String
Dim sBackupFile As String
Dim strFileName As String
Dim sBackupFolder As String
Dim sFinalPath As String

'this will show you the dialog box to select the file you want to backup
strFileName = FindBackUpFile
If Not strFileName = "None Selected" Then
sSourcePath = strFileName
Else
MsgBox " BackUp Action cancelled. Database not backed up. ", vbCritical, " BackUp Failure"
Exit Function
End If

'this will create a temporary directory on "C:\" and will call it Temp if there is no a temp directory
If Not Dir("C:\Temp", vbDirectory) <> "" Then MkDir "C:\Temp"
sBackupPath = "C:\Temp\"
sBackupFile = "BackUp.mdb"

'this will show you the dialog box to select where you want to save the zipped file
sBackupFolder = FindBackUpFolder
If Not sBackupFolder = "None Selected" Then
sFinalPath = sBackupFolder & "\"
Else
MsgBox " BackUp Action cancelled. Database not backed up. ", vbCritical, " BackUp Failure"
Exit Function
End If

'this will make the cursor hour glass shape
Screen.MousePointer = 11

Set fso = New FileSystemObject
fso.CopyFile sSourcePath, sBackupPath & sBackupFile, True
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

'here you can change the name of the file.

sZipFileName = Left(sBackupFile, InStr(1, sBackupFile, ".", vbTextCompare) - 1) & Format(Date, "dd-mm-yyyy") & "-" & Format(Time, "hh-mmAMPM") & ".zip"
sZipFile = sBackupPath & sZipFileName
sFileToZip = sBackupPath & sBackupFile

Call Shell(sWinZip & " -a " & sZipFile & " " & sFileToZip, vbHide)

Pause (3)

Set fso = New FileSystemObject
fso.CopyFile sBackupPath & sZipFileName, sFinalPath & sZipFileName, True
Set fso = Nothing


Screen.MousePointer = 0

MsgBox "Backup was successful. " & "The backup file is named: " & Chr(13) & " " & sFinalPath & sZipFileName, vbInformation, "Backup Completed"

If Dir(sBackupPath & sBackupFile) <> "" Then Kill (sBackupPath & sBackupFile)
If Dir(sBackupPath & sZipFileName) <> "" Then Kill (sBackupPath & sZipFileName)

Exit_BackUpDb:
Exit Function

Err_BackUpDb:
If Err = 5 Then 'Invalid procedure call or argument
MsgBox "Disk is full! Can not move the zip file to the Drive. Please move the " & sZipFile & " file to a safe location.", vbCritical, " BackUp Failure"
If Dir(sBackupPath & sBackupFile) <> "" Then Kill (sBackupPath & sBackupFile)
If Dir(sBackupPath & sZipFileName) <> "" Then Kill (sBackupPath & sZipFileName)
Exit Function
ElseIf Err = 53 Then 'File not found
MsgBox "Source file can not be found!" & vbNewLine & sZipFileName, vbCritical, " BackUp Failure"
Exit Function
ElseIf Err = 71 Then 'Disk not ready
If Dir(sZipFile) <> "" Then Kill sZipFile
If Dir(sFileToZip) <> "" Then Kill sFileToZip
MsgBox "Please insert a diskette in Drive and try again!", vbCritical, " BackUp Failure"
Exit Function
ElseIf Err = -2147024784 Then 'Method 'CopyFile' of object 'IFileSystem3' failed
MsgBox "File is to large to be zipped to the Drive!" & vbNewLine & sZipFile, vbCritical, " BackUp Failure"
Exit Function
Else
MsgBox Err.Number & " - " & Err.Description, , " BackUp Failure"
Resume Exit_BackUpDb
End If

End Function

17 comments:

JP said...

Nice job on the code! The Winzip part looks really useful, I might have to borrow it for some of my projects :-]

Thx,
JP

Raghed said...

ure welcome jp,
i am very happy that i have helped..
i'll be always in service

p.paul said...

Yes its seems to be a great idea to create a backup of your database, as once incidentally i deleted my access database permanently and that time i didnt made backup of that.

That database was very important for me, then i try an access recovery software from here : http://www.repair-access-file.com/access-mdb-recovery.php

after using it i was able to view my database but in preview mode, so i buy the full version first so that i save my database.

So if any of you dont have backup of their access database, and you deleted the database as i did, so this software can help you out.

Raghed said...

thanx paul for ure comment..
and i agree with you that the backup is the very important thing in computer world.
as a programmer i don't feel that i can edit any document or any program without making a backup simultaneously.
and i have to thank you for this tool. i have visited the site and i am very interested.

thnx.

Anonymous said...

If im in the situation of the owner of this blog. I dont know how to post this kind of topic. he has a nice idea.

Raghed said...

thanx for your comment number:)

Anonymous said...

THANK YOU BILLION TIMES FOR SHAREING THIS CODE!!!!! It Helped me a lot! May God Bless You - friend!

Raghed said...

thank you anonymous , i am really impressed for this comment, may god help me publishing more scripts..

Backup Right said...

Having a computer backup is a great idea since a crash can happen pretty much anytime.

Alexis said...

There is a good tool which recover office files and possible more-MDB Repair.It helped me many times and tool is free as far as I know.Moreover utility can preview of the structure of the recovered data and the contents of table columns.

Anonymous said...

Hi. I've used this a lot: thanks. But if you (or two different people) do the backup twice a day, it replaces the first backup. Is there a way to time stamp backups to avoid this problem?

Anonymous said...

Hi. I've used this a lot: thanks. But if you (or two different people) do the backup twice a day, it replaces the first backup. Is there a way to time stamp backups to avoid this problem?

adda644 said...

link to sample is not working.
Plz fix it.

adda644 said...

link to sample is not working.
Plz fix it

Amin Ch said...

Thanks for backup my software payroll

Unknown said...

File is not downloading?

Unknown said...

Get Access File Recovery Software which can easily repairs corrupt MS Access (.MDB or .ACCDB) database file and then recover tables, queries, forms, macros, reports, etc. It also restores permanently deleted data from MDB file of MS Access 2013, 2010, 2007, 2003, 2002, 2000 on your latest Windows 8.1 PC.

Get more details:- http://www.filesrecoverytool.com/access-file-recovery.html