Back-Up MS Access Database in VB.NET Tutorial and Source code

Back-Up MS Access Database in VB.NET Tutorial and Source code

Problem

Create a Windows Form Application program in Visual Basic.Net that will allow users to back-up MS Access Database.

Description

This tutorial will allow the user to create a back-up system for a MS Access Database.

Before the tutorial the following are required to start:

  • Microsoft Visual Studio 2008 – Above

The tutorial starts here:

  1. Open Microsoft Visual Studio 2012
  2. Select a New Project on the File menu.
  3. Select Visual Basic, Windows Form Application then click OK.
  4. We need the following controls in our form:
  • 4 Labels – 2 labels for the labeling of source file and destination file textbox and 2 labels for open file dialogue and save file dialogue.
  • 2 Text box – text boxes for the source file and destination file.
  • 1 Command button – 1 button for backup.
  • 1 Save File Dialogue
  • 1 Open File Dialogue
  1. We will also name our form controls in this way:
  • txtsource is the name of the textbox for username
  • txtdestination is the name of the textbox for Password
  • cmdbackup is the name of the button for Login

the rest are default.

  1. This is how we design the form. (Feel free to layout your own)
Back-Up MS Access Database in VB.NET Tutorial and Source code - Form Design
Back-Up MS Access Database in VB.NET Tutorial and Source code – Form Design

Figure 1. Design of the Form

  1. Paste the code to import a statement

Code here

Imports System.IO

End Code

Code Explanation:

This will allow the reading and writing of files thus it will do so to backup a database.

  1. Double click the windows form and paste the following code.

Code here

txtsource.Text = Application.StartupPath & "\" & "testdb.mdb"
txtdestination.Text = Application.StartupPath & "\" & "Backup_Database\" & "backup_" & Format(Now, "MM-dd-yy") & "@" & Format(Now, "hhmmsstt") & ".mdb"

End Code

Code Explanation:

The code will display the directory on the main database in the source file textbox and the destination directory in the destination file textbox.

  1. Paste these codes for the SaveFileDailogue and label 3.

Code here

Private Sub SaveFileDialog1_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles SaveFileDialog1.FileOk
        If Not SaveFileDialog1.FileName.EndsWith(".mdb") Then
            MsgBox("Invalid Database Format!", MsgBoxStyle.Exclamation)
        Else
            txtdestination.Text = SaveFileDialog1.FileName
        End If
End Sub

Private Sub Label3_Click(sender As Object, e As EventArgs) Handles Label3.Click
        SaveFileDialog1.ShowDialog()
End Sub

End Code

Code Explanation:

If the user clicked “ok” on the save file dialogue and the file it is not a .mdb format, it will call the attention of the user to choose an MS Access database format (.mdb), else the directory of the database will be displayed at the destination file text box. For the click event of label 3, it will show the save file dialogue.

  1. paste the following code for label 4 and open file dialogue

Code here

Private Sub Label4_Click(sender As Object, e As EventArgs) Handles Label4.Click
        OpenFileDialog1.ShowDialog()
End Sub
    
Private Sub OpenFileDialog1_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk
        If Not OpenFileDialog1.FileName.EndsWith(".mdb") Then
            MsgBox("Invalid Database Format!", MsgBoxStyle.Exclamation)
        Else
            txtsource.Text = OpenFileDialog1.FileName
        End If
End Sub

End Code

Code Explanation:

If the user clicked “ok” on the open file dialogue and the selected file it is not a .mdb format, it will call the attention of the user to choose the database, else the directory of the database will be displayed at the source file text box. For the click event of label 4, it will show the open file dialogue.

  1. double click the backup button and paste the following code:

Code here

If txtsource.Text = "" Or txtdestination.Text = "" Then
            MsgBox("Enter source amd destination path!", MsgBoxStyle.Information, "Back Up Utility")
        Else
            System.IO.File.Copy(txtsource.Text, txtdestination.Text)
            MsgBox("Backup Success !!!", MsgBoxStyle.Information)
            Me.Close()
End If

End Code

Code Explanation:

If the source text box and destination text box is empty then user will be informed to choose or enter a source and destination for the database, else the code will back-up the database and inform the user that the database is successfully back-upped.

Back-Up MS Access Database in VB.NET Tutorial and Source code - Final Output
Back-Up MS Access Database in VB.NET Tutorial and Source code – Final Output

Author:

Name: Charlie Devera
Email Address: [email protected]

Free Download Source code (Backup Database in VB.NET)

You may visit our facebook page for more information, inquiries and comments.

Hire our team to do the project.

Comments

comments

, , ,

Post navigation