Results 1 to 6 of 6

Thread: code for backup and restore database in VB6

  1. #1
    Join Date
    Nov 2009
    Posts
    35

    code for backup and restore database in VB6

    Hi,
    As per my syllabus I am working on project which I make in VB6.I had completed 60% of project.I want to include backup and restore database option in my project.But I am not able to do this.That's why I want need help from yours. Please any of you have any idea regarding this then help me.I will be grateful for your help.Thanks.

  2. #2
    Join Date
    Apr 2008
    Posts
    1,948

    Re: code for backup and restore database in VB6

    I write this code for my project to backup and restore database in VB6.So I just post it here for you.Make some necessary changes as per your controls,variables and data types.Do not change the basic concept.



    Dim DBTemperorySource As Database
    Dim DBTemperoryDestination As Database

    Dim RecTempeoryrSource As Recordset
    Dim RecTemperoryDestination As Recordset

    Sub MBackup()

    Set FSO1 = CreateObject("Scripting.FileSystemObject")
    On Error GoTo Errors
    If OptBackup Then
    TextRemarks = "Backup began at " & Time
    TextRemarks = TextRemarks & vbCrLf & "Closing Connection ...!"
    GCnnGeneral.Close
    TextRemarks = TextRemarks & vbCrLf & "Checking Destination ...!"
    If GFileExists(TextDestination) Then
    Kill TxtDestination
    End If

    TextRemarks = TextRemarks & vbCrLf & "Compacting Source ..."
    DBEngine.CompactDatabase TextSource, TextDestination, , , ";pwd=Debasis"
    TextRemarks = TextRemarks & vbCrLf & "Destination Created ...!"
    TextRemarks = TextRemarks & vbCrLf & "Connecting Database ...!"
    With GCnnGeneral
    .Provider = "Microsoft.Jet.OLEDB.4.0"
    .Properties("Jet OLEDB: Database Password") = "Debasis"
    .Mode = adModeReadWrite
    .Open App.Path & "\" & Trim(GFileName) & ".MDB"
    End With
    'GFileName = Trim(LstDatabase.Text)
    TextRemarks = TextRemarks & vbCrLf & "Backup Created at " & Time
    MsgBox "Backup Created."
    TextSource = GEmptyStr
    TextDestination = GEmptyStr
    ElseIf OptRestore Then
    'GCnnAccts.Close
    TextRemarks = "Restoring Data Started at " & Time
    GCnnGeneral.Close
    TextRemarks = TextRemarks & vbCrLf & "Connection Closed ...!"
    Kill TxtDestination
    TextRemarks = TextRemarks & vbCrLf & "Destination Checked ...!"
    Call FSO1.CopyFile(TextSource, TextDestination, True)
    TextRemarks = TextRemarks & vbCrLf & "Data Restored ...!"
    With GCnnGeneral
    .Provider = "Microsoft.Jet.OLEDB.4.0"
    .Properties("Jet OLEDB: Database Password") = "Debasis"
    .Mode = adModeReadWrite
    .Open App.Path & "\" & Trim(GFileName) & ".MDB"
    End With
    TextRemarks = TextRemarks & vbCrLf & "Connection Complete ...!"
    TextRemarks = TextRemarks & vbCrLf & "Data Restored at " & Time
    MsgBox "Data Restored."
    End If

    Exit Sub
    Errors:
    MsgBox "[ErrNo.: " & Err.Number & "] " & Err.Description
    End Sub

    Private Sub CmdBackup_Click()
    If Trim(TextSource) = GEmptyStr Then
    MsgBox "Source Filename Empty."
    Exit Sub
    End If

    If Trim(TextDestination) = GEmptyStr Then
    MsgBox "Destination File is Empty."
    Exit Sub
    End If

    If OptBackup Then
    If Not GFileExists(TextSource) Then
    MsgBox "Source File Does Not Exist! Please Contact Program Vendor."
    Exit Sub
    End If
    If GFileExists(TextDestination) Then
    If MsgBox("Destination File Already Exists! Do you Want to Replace the File?", vbYesNo + vbQuestion) = vbNo Then
    Exit Sub
    End If
    End If
    ElseIf OptRestore Then
    If Not GFileExists(TextSource) Then
    MsgBox "Source File Does Not Exist! Check Filename and Path."
    Exit Sub
    End If
    End If
    Call MBackup
    End Sub

    Private Sub CmdDestinationSearch_Click()
    If OptBackup Then
    CDOpen.DefaultExt = "Bak"
    CDOpen.FileName = "Temp.Bak"
    CDOpen.ShowSave
    TextDestination = CDOpen.FileName
    Else
    TextDestination = Replace(App.Path & "\" & Trim(GFileName) & ".MDB", "\\", "\") 'GFileName
    End If
    End Sub

    Private Sub CmdExit_Click()
    Unload Me
    End Sub

    Private Sub CmdSourceSearch_Click()
    If OptBackup Then
    TextSource = Replace(App.Path & "\" & Trim(GFileName) & ".MDB", "\\", "\") 'GFileName
    Else
    CDOpen.DefaultExt = "Bak"
    CDOpen.FileName = "Temp.Bak"
    CDOpen.ShowOpen
    TextSource = CDOpen.FileName
    End If
    End Sub

    Private Sub Form_Resize()
    Me.Left = (FrmBackground.Width - Me.Width) / 2
    Me.Top = (FrmBackground.Height - Me.Height) / 2
    End Sub

    Private Sub OptAll_Click()
    FraPart.Visible = False
    End Sub

    Private Sub OptBackup_Click()
    CmdBackup.Caption = OptBackup.Caption & " &File"
    TextRemarks = GEmptyStr
    End Sub

    Private Sub OptPart_Click()
    FraPart.Visible = True
    DtpFrom = Format(DateAdd("d", 7, GTransactDate), "dd/MM/yyyy")
    DtpTo = Format(GTransactDate, "dd/MM/yyyy")
    End Sub

    Private Sub OptRestore_Click()
    CmdBackup.Caption = OptRestore.Caption & " &File"
    TextRemarks = GEmptyStr
    End Sub



    I think this will help you.
    Last edited by Praetor; 24-11-2009 at 09:10 PM.

  3. #3
    Join Date
    May 2008
    Posts
    2,012

    Re: code for backup and restore database in VB6

    Use following code to get idea for creating backup and restore database in vb6.Change controls name as per your program.Before write code make connection of database to your program.

    Dim myDataPath As String
    Dim strFileName As String

    myDataPath = "D:\Information\Database.mdb"

    With CommonDialog1
    .Filter = "MS Access DB File | *.mdb"
    .ShowSave
    If Len(.FileName) > 0 Then
    strFileName = .FileName
    Else
    Exit Sub
    End If
    End With

    On Error GoTo Err
    Dim Conn As New DAO.DBEngine
    Dim strSource As String
    Dim strDest As String


    SetAttribute myDataPath, vbNormal
    strSource = "DataSource=" & myDataPath & ""
    strDest = "DataSource=" & strFileName & "dbengine(0)"

    Conn.CompactDatabase strSource, strDest

    Set Conn = Nothing

    Exit Sub
    Err:
    Debug.Print Err.Description
    End Sub


    Write this code under backup button.

  4. #4
    Join Date
    Apr 2008
    Posts
    2,005

    Re: code for backup and restore database in VB6

    I have given you backup and restore program code below just try it.First I had write code for backup and then I had write code for restore.

    Code for backup.write this code for backup button.
    Private Sub cmdBackup_Click()
    On Error GoTo ErrHandler:

    Dim OBackup As SQLDMO.Backup

    gDatabaseName = "Org Metrics Repository"
    Set OBackup = New SQLDMO.Backup
    Set OBackupEvent = OBackup

    OBackup.Database = GDatabaseName


    Dim Year, Month, Day, Hour, Minute, Filename

    Year = Mid(Date, 7, 4)
    Month = Mid(Date, 4, 2)
    Day = Mid(Date, 1, 2)
    Hour = Mid(Time, 1, 2)
    Minute = Mid(Time, 4, 2)

    Filename = Year & Month & Day & "-" & Hour & Minute & ".bak"

    GBkupRstrFileName = Filename
    OBackup.Files = GBkupRstrFileName


    If Len(Dir(GBkupRstrFileName)) > 0 Then
    Kill (GBkupRstrFileName)
    End If


    Screen.MousePointer = vbHourglass


    OBackup.SQLBackup GSQLServer


    Screen.MousePointer = vbDefault

    Set OBackupEvent = Nothing ' disable events
    Set OBackup = Nothing

    MsgBox "Back up Successful to file - """ & Filename & """"

    Exit Sub

    ErrHandler:
    MsgBox "Error " & Err.Description
    Resume Next
    End Sub


    Code for restore.Write this code for restore button.
    Private Sub cmdBackupRestore_Click()
    Dim lngResult As Long
    Dim intRetValue As Integer

    On Error GoTo errHandler

    intRetValue = MsgBox("Restoring this Backup will overwrite the current database. Are you sure you want to Restore this Backup?", vbApplicationModal + vbQuestion + vbYesNo + vbDefaultButton2, "Restore")

    If intRetValue = vbNo Then
    Exit Sub
    End If

    Me.MousePointer = vbHourglass

    filBackup.ListIndex = lstBackup.ListIndex

    lngResult = CopyFile(strBackupPath & filBackup.FileName, strDatabasePath, 0)

    If lngResult = 1 Then
    MsgBox "Database successfully restored.", vbApplicationModal + vbInformation + vbOKOnly, "Restore"
    Call Update_Main
    Else
    MsgBox "Restore operation failed.", vbApplicationModal + vbExclamation + vbOKOnly, "Restore"
    End If

    Me.MousePointer = vbNormal
    Exit Sub

    errHandler:
    Me.MousePointer = vbNormal

    MsgBox Err.Description & " (Error No. " & Err.Number & ").", conBtns, "Backup Error"
    End Sub

  5. #5
    Join Date
    Jan 2011
    Posts
    1

    Re: code for backup and restore database in VB6

    hi,

    what is the code for Update_Main?

    need your help on this..

    thank you.

  6. #6
    Join Date
    Jan 2011
    Posts
    1

    Re: code for backup and restore database in VB6

    also useful to me
    thank all of you

Similar Threads

  1. Replies: 2
    Last Post: 27-04-2012, 08:48 AM
  2. How to Backup & Restore MySQL Database
    By fumble in forum Guides & Tutorials
    Replies: 2
    Last Post: 16-03-2012, 03:32 PM
  3. Replies: 4
    Last Post: 13-12-2010, 10:16 AM
  4. Replies: 3
    Last Post: 03-07-2010, 12:57 PM
  5. Vista Backup and Restore and System Restore points
    By MrChris- in forum Guides & Tutorials
    Replies: 1
    Last Post: 05-01-2009, 09:05 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,711,616,277.99689 seconds with 17 queries