How to backup MS Access database in Visual Basic

Database Backup
Database Backup

How to backup MS Access database in Visual Basic

Welcome to the part 8 of our Visual Basic 6 tutorial

This tutorial will guide us on how to backup ms access database in visual basic.

Before we start, let us first review the previous tutorials.

1. We have learned how to connect Visual Basic into MS Access.
2. Then we have inserted record in MS Access.
3. We learned how to update the record in the database.
4. We also learned how delete a record in the database.
5. Next is to populate Listview control from our database.
6. We’re able to search a record from database and
7. We have learned how to print the records from database.

To be able to start with the next tutorial, kindly download the file from the previous tutorial which is How to print records from database.

Let’s begin:

1. Once you have the file from, kindly extract it and open Project1.vbp.

2. We are going to add a form for our backup feature. On the menu bar kindly click Project and select Add Form, a dialog box will appear, then from the option select Form and click Open to add a new form to the project.

3. Rename and save the form as BackupFrm.

4. The following are the requirements and controls needed for our backup operation.

TextBox – name it as txtBackupPath. This textbox will display the full path you have selected where you want to save the backup of the database.

Command Button – name it as cmdSave with a caption Save.

DriveListBox – name it as Drive1. This control will display the drives available on your computer.

DirListBox – name it as Dir1. DirListBox is an object that will display the list of directories or folders.

The BackupFrm will be like:

backup

5. Next is we’re going to add Microsoft Scripting Runtime in the Reference of the project. Click Project menu and select References, a dialog box will appear, scroll down and check Microsoft Scripting Runtime and click OK.

6. Right click BackupFrm and select View Code. Add the code below to the form.

Private Sub Dir1_Click()
txtBackupPath = Dir1.Path
On Error GoTo errHandler:
Exit Sub
errHandler:
MsgBox " Error drive is not accessible", vbInformation, ""
Drive1.Drive = "C:\"
End Sub
Private Sub Dir1_Change()
Dir1_Click
End Sub
Private Sub Drive1_Change()
On Error GoTo errHandler
Dir1.Path = Drive1.Drive
Exit Sub
errHandler:
MsgBox " Error drive is not accessible", vbInformation, ""
Drive1.Drive = "C:\"
End Sub

7.  After you have added the codes, kindly right click the BackupFrm and select View Object, this will return to the design view of the form.

8. Double click the cmdSave button and add the lines of code below.

Dim FS As New FileSystemObject
FS.CopyFile App.Path & "\Data.mdb", txtBackupPath & "\Data" & ".mdb", True
Set FS = Nothing
MsgBox "Backup successful", vbInformation, ""

Note: before you click the Save button kindly select first the location where you want to save or create a copy of the database. To test if the code is really working, kindly save the backup copy to your desktop. It will prompt a message (Backup successful) indicating that a copy have been created, kindly check your desktop if there is a Data.mdb file, if there is a file then it indicates that the code really works.

9. Open InsertRecordFrm and add a command button, name it to cmdBackup with a Backup caption, then double click the button and add the code below(the code will open the BackupFrm):

BackupFrm.Show 

10. Kindly test the project by pressing F5.

Report it to us if you have found bugs and errors in the project and we will do our best to help you.

Thank you for visiting the site.

, , , , ,

Post navigation

One thought on “How to backup MS Access database in Visual Basic

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.