This is a sample code for taking backup and restore of access database
[code=vb]
Dim DBTempSource As Database
Dim DBTempDestinati on As Database
Dim RecTempSource As Recordset
Dim RecTempDestinat ion As Recordset
Sub MBackup()
Set FSO = CreateObject("S cripting.FileSy stemObject")
On Error GoTo Errors
If OptBackup Then
TxtRemarks = "Backup Started at " & Time
TxtRemarks = TxtRemarks & vbCrLf & "Closing Connection ...!"
GCnnGeneral.Clo se
TxtRemarks = TxtRemarks & vbCrLf & "Checking Destination ...!"
If GFileExists(Txt Destination) Then
Kill TxtDestination
End If
TxtRemarks = TxtRemarks & vbCrLf & "Compacting Source ..."
DBEngine.Compac tDatabase TxtSource, TxtDestination, , , ";pwd=Debas is"
TxtRemarks = TxtRemarks & vbCrLf & "Destinatio n Created ...!"
TxtRemarks = TxtRemarks & vbCrLf & "Connecting Database ...!"
With GCnnGeneral
.Provider = "Microsoft.Jet. OLEDB.4.0"
.Properties("Je t OLEDB:Database Password") = "Debasis"
.Mode = adModeReadWrite
.Open App.Path & "\" & Trim(GFileName) & ".MDB"
End With
'GFileName = Trim(LstDatabas e.Text)
TxtRemarks = TxtRemarks & vbCrLf & "Backup Created at " & Time
MsgBox "Backup Created."
TxtSource = GEmptyStr
TxtDestination = GEmptyStr
ElseIf OptRestore Then
'GCnnAccts.Clos e
TxtRemarks = "Restoring Data Started at " & Time
GCnnGeneral.Clo se
TxtRemarks = TxtRemarks & vbCrLf & "Connection Closed ...!"
Kill TxtDestination
TxtRemarks = TxtRemarks & vbCrLf & "Destinatio n Checked ...!"
Call FSO.CopyFile(Tx tSource, TxtDestination, True)
TxtRemarks = TxtRemarks & vbCrLf & "Data Restored ...!"
With GCnnGeneral
.Provider = "Microsoft.Jet. OLEDB.4.0"
.Properties("Je t OLEDB:Database Password") = "Debasis"
.Mode = adModeReadWrite
.Open App.Path & "\" & Trim(GFileName) & ".MDB"
End With
TxtRemarks = TxtRemarks & vbCrLf & "Connection Complete ...!"
TxtRemarks = TxtRemarks & 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(TxtSource) = GEmptyStr Then
MsgBox "Source Filename Empty."
Exit Sub
End If
If Trim(TxtDestina tion) = GEmptyStr Then
MsgBox "Destinatio n Filename Empty."
Exit Sub
End If
If OptBackup Then
If Not GFileExists(Txt Source) Then
MsgBox "Source File Does Not Exist! Please Contact Program Vendor."
Exit Sub
End If
If GFileExists(Txt Destination) Then
If MsgBox("Destina tion 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(Txt Source) Then
MsgBox "Source File Does Not Exist! Check Filename and Path."
Exit Sub
End If
End If
Call MBackup
End Sub
Private Sub CmdDestinationS earch_Click()
If OptBackup Then
CDOpen.DefaultE xt = "Bak"
CDOpen.FileName = "Temp.Bak"
CDOpen.ShowSave
TxtDestination = CDOpen.FileName
Else
TxtDestination = Replace(App.Pat h & "\" & Trim(GFileName) & ".MDB", "\\", "\") 'GFileName
End If
End Sub
Private Sub CmdExit_Click()
Unload Me
End Sub
Private Sub CmdSourceSearch _Click()
If OptBackup Then
TxtSource = Replace(App.Pat h & "\" & Trim(GFileName) & ".MDB", "\\", "\") 'GFileName
Else
CDOpen.DefaultE xt = "Bak"
CDOpen.FileName = "Temp.Bak"
CDOpen.ShowOpen
TxtSource = 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.Capti on = OptBackup.Capti on & " &File"
TxtRemarks = GEmptyStr
End Sub
Private Sub OptPart_Click()
FraPart.Visible = True
DtpFrom = Format(DateAdd( "d", 7, GTransactDate), "dd/MMM/yyyy")
DtpTo = Format(GTransac tDate, "dd/MMM/yyyy")
End Sub
Private Sub OptRestore_Clic k()
CmdBackup.Capti on = OptRestore.Capt ion & " &File"
TxtRemarks = GEmptyStr
End Sub
[/code]
NOTE:--Users can customize the above code by adding / altering / removing the name of the controls and other parts of the code.
[code=vb]
Dim DBTempSource As Database
Dim DBTempDestinati on As Database
Dim RecTempSource As Recordset
Dim RecTempDestinat ion As Recordset
Sub MBackup()
Set FSO = CreateObject("S cripting.FileSy stemObject")
On Error GoTo Errors
If OptBackup Then
TxtRemarks = "Backup Started at " & Time
TxtRemarks = TxtRemarks & vbCrLf & "Closing Connection ...!"
GCnnGeneral.Clo se
TxtRemarks = TxtRemarks & vbCrLf & "Checking Destination ...!"
If GFileExists(Txt Destination) Then
Kill TxtDestination
End If
TxtRemarks = TxtRemarks & vbCrLf & "Compacting Source ..."
DBEngine.Compac tDatabase TxtSource, TxtDestination, , , ";pwd=Debas is"
TxtRemarks = TxtRemarks & vbCrLf & "Destinatio n Created ...!"
TxtRemarks = TxtRemarks & vbCrLf & "Connecting Database ...!"
With GCnnGeneral
.Provider = "Microsoft.Jet. OLEDB.4.0"
.Properties("Je t OLEDB:Database Password") = "Debasis"
.Mode = adModeReadWrite
.Open App.Path & "\" & Trim(GFileName) & ".MDB"
End With
'GFileName = Trim(LstDatabas e.Text)
TxtRemarks = TxtRemarks & vbCrLf & "Backup Created at " & Time
MsgBox "Backup Created."
TxtSource = GEmptyStr
TxtDestination = GEmptyStr
ElseIf OptRestore Then
'GCnnAccts.Clos e
TxtRemarks = "Restoring Data Started at " & Time
GCnnGeneral.Clo se
TxtRemarks = TxtRemarks & vbCrLf & "Connection Closed ...!"
Kill TxtDestination
TxtRemarks = TxtRemarks & vbCrLf & "Destinatio n Checked ...!"
Call FSO.CopyFile(Tx tSource, TxtDestination, True)
TxtRemarks = TxtRemarks & vbCrLf & "Data Restored ...!"
With GCnnGeneral
.Provider = "Microsoft.Jet. OLEDB.4.0"
.Properties("Je t OLEDB:Database Password") = "Debasis"
.Mode = adModeReadWrite
.Open App.Path & "\" & Trim(GFileName) & ".MDB"
End With
TxtRemarks = TxtRemarks & vbCrLf & "Connection Complete ...!"
TxtRemarks = TxtRemarks & 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(TxtSource) = GEmptyStr Then
MsgBox "Source Filename Empty."
Exit Sub
End If
If Trim(TxtDestina tion) = GEmptyStr Then
MsgBox "Destinatio n Filename Empty."
Exit Sub
End If
If OptBackup Then
If Not GFileExists(Txt Source) Then
MsgBox "Source File Does Not Exist! Please Contact Program Vendor."
Exit Sub
End If
If GFileExists(Txt Destination) Then
If MsgBox("Destina tion 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(Txt Source) Then
MsgBox "Source File Does Not Exist! Check Filename and Path."
Exit Sub
End If
End If
Call MBackup
End Sub
Private Sub CmdDestinationS earch_Click()
If OptBackup Then
CDOpen.DefaultE xt = "Bak"
CDOpen.FileName = "Temp.Bak"
CDOpen.ShowSave
TxtDestination = CDOpen.FileName
Else
TxtDestination = Replace(App.Pat h & "\" & Trim(GFileName) & ".MDB", "\\", "\") 'GFileName
End If
End Sub
Private Sub CmdExit_Click()
Unload Me
End Sub
Private Sub CmdSourceSearch _Click()
If OptBackup Then
TxtSource = Replace(App.Pat h & "\" & Trim(GFileName) & ".MDB", "\\", "\") 'GFileName
Else
CDOpen.DefaultE xt = "Bak"
CDOpen.FileName = "Temp.Bak"
CDOpen.ShowOpen
TxtSource = 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.Capti on = OptBackup.Capti on & " &File"
TxtRemarks = GEmptyStr
End Sub
Private Sub OptPart_Click()
FraPart.Visible = True
DtpFrom = Format(DateAdd( "d", 7, GTransactDate), "dd/MMM/yyyy")
DtpTo = Format(GTransac tDate, "dd/MMM/yyyy")
End Sub
Private Sub OptRestore_Clic k()
CmdBackup.Capti on = OptRestore.Capt ion & " &File"
TxtRemarks = GEmptyStr
End Sub
[/code]
NOTE:--Users can customize the above code by adding / altering / removing the name of the controls and other parts of the code.
Comment