Skip to main content

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

Screenshot
Body
This tutorial, i'm going to show you how to create a simple way on how to create an application that will of enable you to create a Backup of your Microsoft Access Database using Visual Basic.Net. To start in this application, Create a new project in Visual Basic and Name it as “dbBackup”. Then add three buttons, Textbox, three Labels. And arrange all objects that looks like as shown below. backup Next step, from the toolbox, add OpenFileDialog and name it as “OFD” then add also a SaveFileDialog then name it as “SFD”. After designing the user interface, we are now ready to add functionality to our application. To do this, double click the form and add the following code:
  1. 'Set the backup button as disabled
  2. btnbackup.Enabled = False
Next, double click also the browse button, and add the following code: The following code below will simply used to select a specific Microsoft Access database file and set the location to the Textbox provided.
  1. Try
  2. 'set the Title of a Openfolder Dialog Box
  3. OFD.Title = "Please MS Access Database File"
  4. 'Set a specific Filter and Index
  5. OFD.Filter = "MS Access Database Files (*.mdb)|*.accdb"
  6. OFD.FilterIndex = 1
  7. ' when the Browse Image button is click it will open the OpenfileDialog
  8. 'this line of will check if the dialogresult selected is cancel then
  9. If OFD.ShowDialog <> Windows.Forms.DialogResult.Cancel Then
  10. 'Set the location
  11. txtlocation.Text = OFD.FileName
  12.  
  13. End If
  14. Catch ex As Exception
  15. 'catch some error that may occur
  16. MsgBox(ex.Message)
  17. End Try
And we need also to add code to check if the textbox is empty, and if empty it will disable the Backup button, double click the textbox and here’s the following code:
  1.  
  2. ' it Disable the Backup button when the location is empty else do the opposite
  3. If txtlocation.Text = "" Then
  4. btnbackup.Enabled = False
  5.  
  6. Else
  7. btnbackup.Enabled = True
  8. End If
And finally add the following code for Back up button.
  1.  
  2. Try
  3. 'call the SavefileDialog box
  4. SFD.ShowDialog()
  5. 'Set the title
  6. SFD.Title = "Save File"
  7. 'Set a specific filter
  8. SFD.Filter = "(*.mdb)|*.accdb"
  9. If SFD.ShowDialog = Windows.Forms.DialogResult.OK Then
  10. 'set the destination of a file
  11. txtDestination.Text = SFD.FileName
  12. Dim portfolioPath As String = My.Application.Info.DirectoryPath
  13. 'create a backup by using Filecopy Command to copy the file from location to destination
  14. FileCopy(txtlocation.Text, txtDestination.Text)
  15. MsgBox("Database Backup Created Successfully!")
  16. 'Reload the form
  17. Call Form1_Load(sender, e)
  18. txtlocation.Text = Nothing
  19. txtDestination.Text = "Destination..."
  20. End If
  21.  
  22.  
  23. Catch ex As Exception
  24. 'catch some error that may occur
  25. MsgBox(ex.Message)
  26.  
  27. End Try
then press F5, and test your application. Just follow the link if you want to download the Complete Source Code.Click here.

Add new comment