A Simple Backup and Restore Microsoft Access Database Using Visual Basic.Net [Part II]

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.
  1. 'Set the two button as disabled
  2. btnbackup.Enabled = False
  3. btnrestore.Enabled = False
Step 2: Double click the Textbox and change the existing code with this new codes.
  1. ' it Disable the two button when the location is empty else do the opposite
  2. If txtlocation.Text = "" Then
  3. btnbackup.Enabled = False
  4. btnrestore.Enabled = False
  5.  
  6. Else
  7. btnbackup.Enabled = True
  8. btnrestore.Enabled = True
  9.  
  10. End If
Step 3: And for the Restore Button, add the following code:
  1. Try
  2. 'call the SavefileDialog box
  3. SFD.ShowDialog()
  4. 'Set the title
  5. SFD.Title = "Save File"
  6. 'Set a specific filter
  7. SFD.Filter = "(*.mdb)|*.accdb"
  8. If SFD.ShowDialog = Windows.Forms.DialogResult.OK Then
  9. 'set the destination of a file
  10. txtDestination.Text = SFD.FileName
  11. Dim portfolioPath As String = My.Application.Info.DirectoryPath
  12. 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
  13. 'Restore the database from a backup copy.
  14. FileCopy(txtlocation.Text, txtDestination.Text)
  15. MsgBox("Database Restoration Successful")
  16.  
  17. End If
  18.  
  19. 'Reload the form
  20. Call Form1_Load(sender, e)
  21. txtlocation.Text = Nothing
  22. txtDestination.Text = "Destination..."
  23. End If
  24.  
  25.  
  26. Catch ex As Exception
  27. 'catch some error that may occur
  28. MsgBox(ex.Message)
  29.  
  30. End Try
Step 4: Test your application by pressing F5 button. And here’s the following code used in this application:
  1. Public Class Form1
  2.  
  3. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnbackup.Click
  4.  
  5. Try
  6. 'call the SavefileDialog box
  7. SFD.ShowDialog()
  8. 'Set the title
  9. SFD.Title = "Save File"
  10. 'Set a specific filter
  11. SFD.Filter = "(*.mdb)|*.accdb"
  12. If SFD.ShowDialog = Windows.Forms.DialogResult.OK Then
  13. 'set the destination of a file
  14. txtDestination.Text = SFD.FileName
  15. Dim portfolioPath As String = My.Application.Info.DirectoryPath
  16. 'create a backup by using Filecopy Command to copy the file from location to destination
  17. FileCopy(txtlocation.Text, txtDestination.Text)
  18. MsgBox("Database Backup Created Successfully!")
  19. 'Reload the form
  20. Call Form1_Load(sender, e)
  21. txtlocation.Text = Nothing
  22. txtDestination.Text = "Destination..."
  23. End If
  24.  
  25.  
  26. Catch ex As Exception
  27. 'catch some error that may occur
  28. MsgBox(ex.Message)
  29.  
  30. End Try
  31.  
  32. End Sub
  33.  
  34. Private Sub btnBrowse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBrowse.Click
  35. Try
  36. 'set the Title of a Openfolder Dialog Box
  37. OFD.Title = "Please MS Access Database File"
  38. 'Set a specific Filter and Index
  39. OFD.Filter = "MS Access Database Files (*.mdb)|*.accdb"
  40. OFD.FilterIndex = 1
  41. ' when the Browse Image button is click it will open the OpenfileDialog
  42. 'this line of will check if the dialogresult selected is cancel then
  43. If OFD.ShowDialog <> Windows.Forms.DialogResult.Cancel Then
  44. 'Set the location
  45. txtlocation.Text = OFD.FileName
  46.  
  47. End If
  48. Catch ex As Exception
  49. 'catch some error that may occur
  50. MsgBox(ex.Message)
  51. End Try
  52. End Sub
  53.  
  54. Private Sub txtlocation_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtlocation.TextChanged
  55. ' it Disable the two button when the location is empty else do the opposite
  56. If txtlocation.Text = "" Then
  57. btnbackup.Enabled = False
  58. btnrestore.Enabled = False
  59.  
  60. Else
  61. btnbackup.Enabled = True
  62. btnrestore.Enabled = True
  63.  
  64. End If
  65.  
  66. End Sub
  67.  
  68. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  69. 'Set the two button as disabled
  70. btnbackup.Enabled = False
  71. btnrestore.Enabled = False
  72.  
  73. End Sub
  74.  
  75.  
  76. Private Sub btnrestore_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnrestore.Click
  77. Try
  78. 'call the SavefileDialog box
  79. SFD.ShowDialog()
  80. 'Set the title
  81. SFD.Title = "Save File"
  82. 'Set a specific filter
  83. SFD.Filter = "(*.mdb)|*.accdb"
  84. If SFD.ShowDialog = Windows.Forms.DialogResult.OK Then
  85. 'set the destination of a file
  86. txtDestination.Text = SFD.FileName
  87. Dim portfolioPath As String = My.Application.Info.DirectoryPath
  88. 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
  89. 'Restore the database from a backup copy.
  90. FileCopy(txtlocation.Text, txtDestination.Text)
  91. MsgBox("Database Restoration Successful")
  92.  
  93. End If
  94.  
  95. 'Reload the form
  96. Call Form1_Load(sender, e)
  97. txtlocation.Text = Nothing
  98. txtDestination.Text = "Destination..."
  99. End If
  100.  
  101.  
  102. Catch ex As Exception
  103. 'catch some error that may occur
  104. MsgBox(ex.Message)
  105.  
  106. End Try
  107. End Sub
  108. End Class
Just follow the link if you want to download the Complete Source Code.Click here.

Comments

Submitted bysouksamay (not verified)on Fri, 11/24/2017 - 16:32

this code is very good

Add new comment