A Simple Backup and Restore Microsoft Access Database Using Visual Basic.Net [Part II]
Submitted by joken on Thursday, November 6, 2014 - 09:09.
The lesson is the continuation of our last tutorial called “A Simple Backup and Restore Microsoft Access Database Using Visual Basic.Net [Part II]”, but this time we’re going to focus on how to Restore a MS Access Database. To start with this course, open our last project called “dbBackup”. Then do the following steps:
Step 1: Double click the form, then modify the existing code using this new codes.
Step 2: Double click the Textbox and change the existing code with this new codes.
Step 3: And for the Restore Button, add the following code:
Step 4: Test your application by pressing F5 button.
And here’s the following code used in this application:
Just follow the link if you want to download the Complete Source Code.Click here.
- 'Set the two button as disabled
- btnbackup.Enabled = False
- btnrestore.Enabled = False
- ' it Disable the two button when the location is empty else do the opposite
- If txtlocation.Text = "" Then
- btnbackup.Enabled = False
- btnrestore.Enabled = False
- Else
- btnbackup.Enabled = True
- btnrestore.Enabled = True
- End If
- Try
- 'call the SavefileDialog box
- SFD.ShowDialog()
- 'Set the title
- SFD.Title = "Save File"
- 'Set a specific filter
- SFD.Filter = "(*.mdb)|*.accdb"
- If SFD.ShowDialog = Windows.Forms.DialogResult.OK Then
- 'set the destination of a file
- txtDestination.Text = SFD.FileName
- Dim portfolioPath As String = My.Application.Info.DirectoryPath
- If MessageBox.Show("Restoring the database will erase any changes you have made since you last backup. Are you sure you want to do this?", "Confirm Delete", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) = Windows.Forms.DialogResult.OK Then
- 'Restore the database from a backup copy.
- End If
- 'Reload the form
- Call Form1_Load(sender, e)
- txtlocation.Text = Nothing
- txtDestination.Text = "Destination..."
- End If
- Catch ex As Exception
- 'catch some error that may occur
- End Try
- Public Class Form1
- Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnbackup.Click
- Try
- 'call the SavefileDialog box
- SFD.ShowDialog()
- 'Set the title
- SFD.Title = "Save File"
- 'Set a specific filter
- SFD.Filter = "(*.mdb)|*.accdb"
- If SFD.ShowDialog = Windows.Forms.DialogResult.OK Then
- 'set the destination of a file
- txtDestination.Text = SFD.FileName
- Dim portfolioPath As String = My.Application.Info.DirectoryPath
- 'create a backup by using Filecopy Command to copy the file from location to destination
- 'Reload the form
- Call Form1_Load(sender, e)
- txtlocation.Text = Nothing
- txtDestination.Text = "Destination..."
- End If
- Catch ex As Exception
- 'catch some error that may occur
- End Try
- End Sub
- Private Sub btnBrowse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBrowse.Click
- Try
- 'set the Title of a Openfolder Dialog Box
- OFD.Title = "Please MS Access Database File"
- 'Set a specific Filter and Index
- OFD.Filter = "MS Access Database Files (*.mdb)|*.accdb"
- OFD.FilterIndex = 1
- ' when the Browse Image button is click it will open the OpenfileDialog
- 'this line of will check if the dialogresult selected is cancel then
- If OFD.ShowDialog <> Windows.Forms.DialogResult.Cancel Then
- 'Set the location
- txtlocation.Text = OFD.FileName
- End If
- Catch ex As Exception
- 'catch some error that may occur
- End Try
- End Sub
- Private Sub txtlocation_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtlocation.TextChanged
- ' it Disable the two button when the location is empty else do the opposite
- If txtlocation.Text = "" Then
- btnbackup.Enabled = False
- btnrestore.Enabled = False
- Else
- btnbackup.Enabled = True
- btnrestore.Enabled = True
- End If
- End Sub
- Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
- 'Set the two button as disabled
- btnbackup.Enabled = False
- btnrestore.Enabled = False
- End Sub
- Private Sub btnrestore_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnrestore.Click
- Try
- 'call the SavefileDialog box
- SFD.ShowDialog()
- 'Set the title
- SFD.Title = "Save File"
- 'Set a specific filter
- SFD.Filter = "(*.mdb)|*.accdb"
- If SFD.ShowDialog = Windows.Forms.DialogResult.OK Then
- 'set the destination of a file
- txtDestination.Text = SFD.FileName
- Dim portfolioPath As String = My.Application.Info.DirectoryPath
- If MessageBox.Show("Restoring the database will erase any changes you have made since you last backup. Are you sure you want to do this?", "Confirm Delete", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) = Windows.Forms.DialogResult.OK Then
- 'Restore the database from a backup copy.
- End If
- 'Reload the form
- Call Form1_Load(sender, e)
- txtlocation.Text = Nothing
- txtDestination.Text = "Destination..."
- End If
- Catch ex As Exception
- 'catch some error that may occur
- End Try
- End Sub
- End Class
Comments
Add new comment
- Add new comment
- 1042 views