Backup and Restore Database using VB6


One issue on creating a system is the database, when your database goes down and it crushes what are the solutions that your system might do, this would be the basis of measuring the reliability of a system, when a database goes down is there any way to recover its content partially, or is there any way to make a copy of the database on a separate drive of location on your computer?
There are solutions that we may do first is to create a function on our system to create a copy of the database or also we call this as backup and when the database crushes you may restore its database, take note that if you will restore a database you cannot restore all of the content from the original one because the records that we may restore are the data which had been saved at the moment that we backup our database.

This feature is a matter of Copying a File to another Location
Let me define first some codes before we start
FileCopy Source,Destination
Source where is our database located usually on our default folder, and destination is where we want to put our Backup, Note that FileCopy is a built in function on vb6 that enable you to copy a file
App.path
                Gets the specific  Location of our project
New controls to be used
                CommonDialog-  can be used to browse for a specific location it’s like the save as and open on Microsoft Word

Now let’s do some design
1.       Just create a Form Containing two button first button would have a caption “Backup” and the second Button is “Restore”
2.       Add some components press ctrl+t then check this component 
3.       Then add this control on your Form located on the Tool box


Now let’s do the coding
                I let the name of the controls to its default name, on button backup put this line of codes
1.       Me.CommonDialog1.Filter = "*.mdb"
This line code just declare that you can only save the file as .mdb
2.       Me.CommonDialog1.ShowSave
IT shows the dialog box to located where do you want to save the file and what would be the name of the file to be backup
3.       If Me.CommonDialog1.FileName <> "" Then
Checks whether open button is clicked
4.       FileCopy App.Path & "\\Data\\db_SADStore.mdb", Me.CommonDialog1.FileName & ".mdb"
A code to copy the file on another location the source is on the data folder on my project meaning the structure of my  project is Default Folder>> Data>> db_SADStore.mdb
5.       MsgBox "Database had been successfully Saved on " & Me.CommonDialog1.FileName & ".mdb"
It just inform the user that the file is saved on  that specific location
6.       End if
End of the code

                Take note that if you are dealing with  an error Access Denied there are some reasons first is to close your  ms access file it might be open, second is you need to close the connection on your code you might want to close it use the ConnectionName.Close then open it after doing a backup and lastly you must run your vb6 as administrator.

                Now Backup is done it’s up for you to do the restore function it’s just the reverse On FileCopy just make the destination be the source and the Source would be the destination. But take note remove the “.mdb” on the destination on backup and change Me.CommonDialog1.ShowSave to Me.CommonDialog1.ShowOpen
Think Logically hahah. J

Main form
Dialog appears when Backup button is clicked

Dialog show the path of the backup file
Happy Coding 

51 komento:

  1. haba ng codes nmin dati d2 sir aa:))2ng yang code mo sir sbrang ikli tas dali pa maintindhan., sayng d nmn naabutan 2ng blogspot mo na 2 sir nung my VB pa kmi.,

    TumugonBurahin
  2. ayos lang yan atleast ngayon may reference kayo anytime anywhere as long as may net

    TumugonBurahin
  3. Thank you for your kind information to teach us about Data Back up in VB6.
    How can i make restore in vb6. please give us complete details as data back up ?

    TumugonBurahin
  4. the restore function for the database is just the reverse of creating a backup you need to close the connection then browse for the database(using open file dialog) you created a backup then replace the existing database on your default folder

    TumugonBurahin
    Mga Tugon
    1. hi sir i am gelo can you help me to code the restore function, the logic is there but i am having a hard time to do it. thank you

      Burahin
    2. Naalis ng may-ari ang komentong ito.

      Burahin
    3. thank you for your idea and logic,., =))

      to share you the restore function, here's my code

      'Note: Click Project ->Refference -> Microsoft Scripting Runtime
      ' Click it to check mark and click OK
      Private Sub DataTransfer()
      Dim Src As String
      Dim Dest As String

      Me.CommonDialog1.Filter "*.mdb" 'to filter the file
      Me.CommonDialog1.ShowOpen 'to show where you want to fetch/get your backup

      Src = Me.CommonDialog1.FileName &".mdb" 'source
      Dest = "C:\database\Data.mdb" 'the database location

      Set FSO = New FileSystemObject
      With FSO

      If .FileExists(Src) Then

      .CopyFile Src, Dest, True
      MsgBox "YOur database has been Restore"

      Else

      MsgBox "Could not restore..."

      End If
      End With
      End Sub

      Burahin
  5. tnx for reading AHAMED NAUFAL ARIFEEN SYED MOHAIDEEN :) happy coding

    TumugonBurahin
  6. How can i use Autocomplete Text in vb6 ?

    TumugonBurahin
  7. Can you please publish the coding for Auto complete text from ms access data base ?

    TumugonBurahin
  8. Please Check the coding
    'RESTORE

    Private Sub Command2_Click()

    Me.CommonDialog1.Filter = "*.mdb"
    Me.CommonDialog1.ShowOpen
    If Me.CommonDialog1.FileName <> "" Then

    FileDestination App.Path & "\DB\nok.mdb"
    MsgBox "Database had been successfully Restored "
    End If
    End Sub

    TumugonBurahin
  9. you have no code for copying the file to tour default folder

    'this code should be putted inside your if condition
    FileCopy(defaultfolder, then the value of the openfiledialog on commonfiledialog.filename)

    '
    then it should work
    take note that all of connection should be closed in order for you to restore the database

    TumugonBurahin
  10. ok I'll try to do some article on auto complete textbox

    TumugonBurahin
  11. what do you mean with auto complete does it mean that will predict the text that you will input from the textbox?? can you give me some details so that i can make an article on it.. tnx

    TumugonBurahin
  12. If i give the input into the text box, the list if stock open by combo box or list box, just if we choose from combo or list box the stock name will be post into the text box.


    For Example:

    Text Box : Ap

    Combo or List : Apple
    Ambula
    A


    TumugonBurahin
  13. ah are you refering to the database if you are using database? IF SO I'LL make a article for you.. :)

    TumugonBurahin
  14. Yes I am using MS Access database ....

    TumugonBurahin
  15. send me your email then ill send you how to do it i cant post it in here because this is intended for my student tolentinojoven@gmail.com thats my email add ill pm u the tutorial :)

    TumugonBurahin
  16. Private Sub Restore Click()

    Help me to restore :


    Me.CommonDialog1.Filter = "*.mdb"
    Me.CommonDialog1.ShowOpen
    If Filedestination(App.Path & "\DB\naufal.mdb", Me.CommonDialog1.FileName <> "") Then

    MsgBox "Database had been successfully Restored "
    End If
    End Sub

    TumugonBurahin
  17. HELP ME TO RESTORE :


    Private Sub Restore Click()

    Me.CommonDialog1.Filter = "*.mdb"
    Me.CommonDialog1.ShowOpen
    If Filedestination(App.Path & "\DB\naufal.mdb", Me.CommonDialog1.FileName <> "") Then

    MsgBox "Database had been successfully Restored"
    End If

    End Sub

    TumugonBurahin
  18. Your if condition is wrong, base your code on the backup code its just the same hmmm...

    just reverse the code thats all you must do in order to have the code functioning.
    consider this algo
    1. your if condition must check if the path is empty
    2. FileCopy("the source where you will get the database", "then your default path for your database")

    TumugonBurahin
  19. So, Can you please send the code ?

    TumugonBurahin
  20. HOW TO ADD BACKUP TAKEN FROM DIFFERENT VERSION OF MDI APPLICATION TO ONE DATABASE USING RESTORE?

    TumugonBurahin
  21. Can you please send me the coding for Restore ?

    TumugonBurahin
  22. @ AHAMED I HAVENT CODE IT YET......
    I WANT TO DISTRIBUTE THE MDI VERSIONS USED AT DIFFERENT LOCATIONS AND DATA STORED AT THESE DIFFERENT LOCATION HAS TO BE MERGED IN A MASTER DATABASE DATEWISE.

    ACTUALLY I M CONFUSE IF IT CAN BE IMPLEMENTED BY RESTORE.....

    AND THANKS FOR THE CODE FOR BACKUP....:)

    TumugonBurahin
  23. i havent use any code yet for restore as what i wanna do is different from restore......

    TumugonBurahin
  24. sir can you give another codes of backup and restore using mysql database? because i'm having a hard time for coding it.

    FileCopy App.Path & "\\Data\\db_SADStore.mdb", Me.CommonDialog1.FileName & ".mdb"

    i tried to copy your code and replace the .mdb to .sql but there's an error after i run the program. the error said "path not found"..

    can you help me?..please.

    TumugonBurahin
  25. encountered an error "Permission denied" ? any help sir?

    TumugonBurahin
  26. Me.CommonDialog1.ShowSave
    If Me.CommonDialog1.FileName <> "" Then
    FileCopy App.Path & "\\Data\\db_SADStore.mdb", Me.CommonDialog1.FileName & ".mdb"
    MsgBox "Database had been successfully Saved on " & Me.CommonDialog1.FileName & ".mdb"

    e2 lng nmn ung mga babaguhin eh ^^

    TumugonBurahin
    Mga Tugon
    1. hehhehe dapat hindi ituro lahat para makita kung natututo ang mag viewers db?.. good Rinkashi :)

      Burahin
  27. Salamat dito boss.Short,precise and easy to understand for beginners like me.keep sharing.Kung pwede gawa din kayo ng sample project for making reports.Thanks Again :D

    TumugonBurahin
  28. sir kaya ba n2 i-restore nang backup ung deleted n database

    TumugonBurahin
  29. I tried gelo Avena's code. Its working fine. Many thanks
    I need to add date and time stamp in front of the file name when i save. is there any way to add this by the program.


    TumugonBurahin
  30. Me.CommonDialog1.Filter = "*.mdb"
    Me.CommonDialog1.ShowSave
    If Me.CommonDialog1.FileName <> "" Then
    FileCopy App.Path & "C:\Users\winnie\Desktop\HOTEL\dbHotel.mdb", Me.CommonDialog1.FileName & ".mdb"
    MsgBox "Database had been successfully Saved on " & Me.CommonDialog1.FileName & ".mdb"
    End If

    ERROR(52): BAD FILE NAME OR NUMBER

    sir ano pong tamang gawin pra po mwala n ung erro? THANK YOU SO MUCH SIR.

    TumugonBurahin
  31. Sir! Paano po ibaback up yyung database while it is running. napansin ko po na nagbaback up sya kung hindi nagrurun yung database pero pag running sya eh nagpepermission denied sya.

    TumugonBurahin
  32. Data report Codes ka brothers!!!!!!!!!

    button codes

    Report.Sections("Section4").Controls("IDNo").Caption = Form3.Text1.Text
    Report.Sections("Section4").Controls("EmployeeName").Caption = Form3.Text2.Text
    Report.Sections("Section4").Controls("Position").Caption = Form3.Text3.Text
    Report.Sections("Section4").Controls("DateBirth").Caption = Form3.DTPicker3.Value
    Report.Sections("Section4").Controls("Detachment").Caption = Form3.Text5.Text
    Report.Sections("Section4").Controls("Attainment").Caption = Form3.Combo5.Text
    Report.Sections("Section4").Controls("TinNo").Caption = Form3.Text8.Text
    Report.Sections("Section4").Controls("Philhealth").Caption = Form3.Text9.Text
    Report.Sections("Section4").Controls("Pagibig").Caption = Form3.Text10.Text
    Report.Sections("Section4").Controls("SSSNo").Caption = Form3.Text11.Text
    Report.Sections("Section4").Controls("ContactNo").Caption = Form3.Text12.Text
    Report.Sections("Section4").Controls("LicenceNo").Caption = Form3.Text13.Text
    Report.Sections("Section4").Controls("LicenceExpiration").Caption = Form3.DTPicker2.Value
    Report.Sections("Section4").Controls("SBRNo").Caption = Form3.Text14.Text
    Report.Sections("Section4").Controls("SosiaCertificate").Caption = Form3.Combo10.Text
    Report.Sections("Section4").Controls("DateHired").Caption = Form3.DTPicker1.Value
    Report.Sections("Section4").Controls("DateExpiry").Caption = Form3.DTPicker.Value

    TumugonBurahin
  33. paano po yung restore dun sa code niyo po ? pinaikot ikot ko na po kasi eh, and promise po, ang sakit na ng ulot ko po. :)

    Private Sub Command2_Click()
    Me.CommonDialog1.Filter = "*.mdb"
    Me.CommonDialog1.ShowSave
    If Me.CommonDialog1.FileName <> "" Then
    FileCopy App.Path & "sample1.mdb", Me.CommonDialog1.FileName & ".mdb"
    MsgBox "Database had been successfully Saved on " & Me.CommonDialog1.FileName & ".mdb"
    End If
    End Sub

    please po. :)

    TumugonBurahin
  34. permission denied.. what should i do? sir

    TumugonBurahin
  35. nice tutorial! nahilo ko.. mejo madaming err sa una. pero ngayun ok na. BIG THANKS author! *LIKE*

    TumugonBurahin
  36. Please subscribe to my Youtube channel now im doing some basic tutorials in c# started to create video and planning to create a shorter versions
    https://www.youtube.com/channel/UCItlPcLVv1ipGn8dXBiAgWw

    click on the link and hit the subscribe button thanks Happy Coding!

    TumugonBurahin