473,405 Members | 2,282 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,405 developers and data experts.

Backup / Restore of Access Database

debasisdas
8,127 Expert 4TB
This is a sample code for taking backup and restore of access database

Expand|Select|Wrap|Line Numbers
  1. Dim DBTempSource As Database
  2. Dim DBTempDestination As Database
  3.  
  4. Dim RecTempSource As Recordset
  5. Dim RecTempDestination As Recordset
  6.  
  7. Sub MBackup()
  8.  
  9. Set FSO = CreateObject("Scripting.FileSystemObject")
  10. On Error GoTo Errors
  11. If OptBackup Then
  12.     TxtRemarks = "Backup Started at " & Time
  13.     TxtRemarks = TxtRemarks & vbCrLf & "Closing Connection ...!"
  14.     GCnnGeneral.Close
  15.     TxtRemarks = TxtRemarks & vbCrLf & "Checking Destination ...!"
  16.     If GFileExists(TxtDestination) Then
  17.         Kill TxtDestination
  18.     End If
  19.  
  20.     TxtRemarks = TxtRemarks & vbCrLf & "Compacting Source ..."
  21.     DBEngine.CompactDatabase TxtSource, TxtDestination, , , ";pwd=Debasis"
  22.     TxtRemarks = TxtRemarks & vbCrLf & "Destination Created ...!"
  23.     TxtRemarks = TxtRemarks & vbCrLf & "Connecting Database ...!"
  24.     With GCnnGeneral
  25.        .Provider = "Microsoft.Jet.OLEDB.4.0"
  26.        .Properties("Jet OLEDB:Database Password") = "Debasis"
  27.        .Mode = adModeReadWrite
  28.        .Open App.Path & "\" & Trim(GFileName) & ".MDB"
  29.     End With
  30.     'GFileName = Trim(LstDatabase.Text)
  31.     TxtRemarks = TxtRemarks & vbCrLf & "Backup Created at " & Time
  32.     MsgBox "Backup Created."
  33.     TxtSource = GEmptyStr
  34.     TxtDestination = GEmptyStr
  35. ElseIf OptRestore Then
  36.     'GCnnAccts.Close
  37.     TxtRemarks = "Restoring Data Started at " & Time
  38.     GCnnGeneral.Close
  39.     TxtRemarks = TxtRemarks & vbCrLf & "Connection Closed ...!"
  40.     Kill TxtDestination
  41.     TxtRemarks = TxtRemarks & vbCrLf & "Destination Checked ...!"
  42.     Call FSO.CopyFile(TxtSource, TxtDestination, True)
  43.     TxtRemarks = TxtRemarks & vbCrLf & "Data Restored ...!"
  44.     With GCnnGeneral
  45.        .Provider = "Microsoft.Jet.OLEDB.4.0"
  46.        .Properties("Jet OLEDB:Database Password") = "Debasis"
  47.        .Mode = adModeReadWrite
  48.        .Open App.Path & "\" & Trim(GFileName) & ".MDB"
  49.     End With
  50.     TxtRemarks = TxtRemarks & vbCrLf & "Connection Complete ...!"
  51.     TxtRemarks = TxtRemarks & vbCrLf & "Data Restored at " & Time
  52.     MsgBox "Data Restored."
  53. End If
  54.  
  55. Exit Sub
  56. Errors:
  57.     MsgBox "[ErrNo.: " & Err.Number & "] " & Err.Description
  58. End Sub
  59.  
  60. Private Sub CmdBackup_Click()
  61. If Trim(TxtSource) = GEmptyStr Then
  62.     MsgBox "Source Filename Empty."
  63.     Exit Sub
  64. End If
  65.  
  66. If Trim(TxtDestination) = GEmptyStr Then
  67.     MsgBox "Destination Filename Empty."
  68.     Exit Sub
  69. End If
  70.  
  71. If OptBackup Then
  72.     If Not GFileExists(TxtSource) Then
  73.         MsgBox "Source File Does Not Exist! Please Contact Program Vendor."
  74.         Exit Sub
  75.     End If
  76.     If GFileExists(TxtDestination) Then
  77.         If MsgBox("Destination File Already Exists! Do you Want to Replace the File?", vbYesNo + vbQuestion) = vbNo Then
  78.             Exit Sub
  79.         End If
  80.     End If
  81. ElseIf OptRestore Then
  82.     If Not GFileExists(TxtSource) Then
  83.         MsgBox "Source File Does Not Exist! Check Filename and Path."
  84.         Exit Sub
  85.     End If
  86. End If
  87. Call MBackup
  88. End Sub
  89.  
  90. Private Sub CmdDestinationSearch_Click()
  91. If OptBackup Then
  92.     CDOpen.DefaultExt = "Bak"
  93.     CDOpen.FileName = "Temp.Bak"
  94.     CDOpen.ShowSave
  95.     TxtDestination = CDOpen.FileName
  96. Else
  97.     TxtDestination = Replace(App.Path & "\" & Trim(GFileName) & ".MDB", "\\", "\")  'GFileName
  98. End If
  99. End Sub
  100.  
  101. Private Sub CmdExit_Click()
  102. Unload Me
  103. End Sub
  104.  
  105. Private Sub CmdSourceSearch_Click()
  106. If OptBackup Then
  107.     TxtSource = Replace(App.Path & "\" & Trim(GFileName) & ".MDB", "\\", "\")    'GFileName
  108. Else
  109.     CDOpen.DefaultExt = "Bak"
  110.     CDOpen.FileName = "Temp.Bak"
  111.     CDOpen.ShowOpen
  112.     TxtSource = CDOpen.FileName
  113. End If
  114. End Sub
  115.  
  116. Private Sub Form_Resize()
  117. Me.Left = (FrmBackground.Width - Me.Width) / 2
  118. Me.Top = (FrmBackground.Height - Me.Height) / 2
  119. End Sub
  120.  
  121. Private Sub OptAll_Click()
  122. FraPart.Visible = False
  123. End Sub
  124.  
  125. Private Sub OptBackup_Click()
  126. CmdBackup.Caption = OptBackup.Caption & " &File"
  127. TxtRemarks = GEmptyStr
  128. End Sub
  129.  
  130. Private Sub OptPart_Click()
  131. FraPart.Visible = True
  132. DtpFrom = Format(DateAdd("d", 7, GTransactDate), "dd/MMM/yyyy")
  133. DtpTo = Format(GTransactDate, "dd/MMM/yyyy")
  134. End Sub
  135.  
  136. Private Sub OptRestore_Click()
  137. CmdBackup.Caption = OptRestore.Caption & " &File"
  138. TxtRemarks = GEmptyStr
  139. End Sub
  140.  
NOTE:--Users can customize the above code by adding / altering / removing the name of the controls and other parts of the code.
Mar 17 '08 #1
1 15255
Thanks for replay.
but this code not explaining the main objective of closing the data base
dim dbs of database
set dbs = "c:\dbsmdb"
like dbs.close
that means i trying to close an instance of MS access opened at address at "c:\dbsmdb"
please advise
Nov 5 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: James | last post by:
HI, I'm looking for a script that will allow users/admins to have a one click backup solution for a MYSQL Database.. 'BACK DATABASE' button, click and its done... The a restore option, that...
2
by: emmexx | last post by:
I want to restore a huge database into my workstation. The size of the backup file is more than 6 GB and I don't have enough space on my HD for both the database and the backup file. So I put the...
3
by: Tim Morrison | last post by:
MSDE2000 I have an application in which I am running a TSQL command of BACKUP DATABASE and RESTORE DATABASE for the backup and restore commands for my application. For testing purposes, i did the...
2
by: Spieg | last post by:
I've a program with VB6 working with SQLServer 2000. I have a Central Station with the program runnin with full prvileges and that can perform all the actions on the DB. There are also some...
1
by: gcetti | last post by:
Can anyone recommend backup procedures for a SQL Server/MSDE product? We have upgraded our program from Access to SQL & before we could just tell our customers what .mdb files to include on their...
6
by: Eric Herber | last post by:
I've a question regarding db2 (V8.1) and database backups going to a storage manager like TSM for example. As I can see in the storage manager if I backup the complete database over the TSM API...
4
by: Hardy | last post by:
hi gurus, now I have to backup and restore a 8 T size db2 database. from two s85 to two 670. the partitions,tablespaces of the db should be redesigned then I plan to use redirected restore. but...
0
by: raj.raghavan | last post by:
Hi, I have a database were all the indexes are in a seperate filegroup a few large tables are in a seperate filegroup. We take backup at different times and I have backup of primary, two filegroup...
10
by: Konstantin Andreev | last post by:
Hello. Some time ago I asked in this conference, - How to use an ONLINE BACKUP to restore database onto another system? - but got no answers. Therefore I can conclude it is not possible. But......
2
by: Divakar | last post by:
Hi All, We are trying to restore a backup that we took last night on a fresh new Tape. The backup was successful but the restore is failing with the following error: db2 =restore db gpiecuk...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.