473,396 Members | 1,689 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How to make a backup of access 2003 database using a command button?

Hello,
I need the procedure and/or code to add a command button to the Main Switchboard form, so the user click it and creates a full backup of the database in other folder, where the database name will have the date added to it.

I have tried different things, with no success... getting depress now!

if I press tools, database utilities, and backup database I'll get what I want, ... well I need to do the same, but using a command button.

(paramount will be if I can do the same without the user intervention, just checkind the date and if has been 7 days since the last backup, trigger a new one automatically)

many thanks in advance
Juan
Feb 4 '10 #1

✓ answered by ADezii

The following code will Backup all Database Objects to a Folder specified by the Constant conPATH_TO_BKUPS (Line #3). You can change this if you wish, but be sure to add the Trailing Backslash since I did not test for this for the sake of brevity. The actual Back Up Database will be the Base Name of the Current Database minus the .mdb Extension, plus an Underscore (_), plus the Date formatted as mmddyyyy, plus .mdb. Each Backup will be unique only as far as a single Day is concerned and will DELETE a previous Backup for that Day should it exist. I intentionally placed the code in a Public Function where it can easily and directly be called from a Main Switchboard Item. Have fun, and if you have any questions, please feel free to ask.
Expand|Select|Wrap|Line Numbers
  1. Public Function fExportAllDBObjects()
  2. On Error Resume Next
  3. Const conPATH_TO_BKUPS As String = "C:\DB Backups\"
  4. Dim strAbsoluteBkUpPath As String
  5. Dim aob As AccessObject
  6. Dim strDBName As String
  7. Dim wrkSpace As Workspace
  8. Dim dbBackup As Database
  9.  
  10. DoCmd.Hourglass True
  11.  
  12. 'Retrive the Current Database Name only, strip out .mdb
  13. strDBName = Replace(Mid$(CurrentDb.Name, InStrRev(CurrentDb.Name, "\") + 1), ".mdb", "")
  14.  
  15. 'Make the Backup DB Name unique for each Date
  16. strDBName = strDBName & "_" & Format$(Date, "mmddyyyy") & ".mdb"
  17.  
  18. strAbsoluteBkUpPath = conPATH_TO_BKUPS & strDBName
  19.  
  20. 'If a Bacup already exists for this Date, then DELETTE it
  21. If Dir$(strAbsoluteBkUpPath) <> "" Then
  22.   Kill strAbsoluteBkUpPath
  23. End If
  24.  
  25. 'Get Default Workspace.
  26. Set wrkSpace = DBEngine.Workspaces(0)
  27.  
  28. 'Create the Database
  29. Set dbBackup = wrkSpace.CreateDatabase(strAbsoluteBkUpPath, dbLangGeneral)
  30.  
  31. 'Export all Tables
  32. For Each aob In CurrentData.AllTables
  33.   If Mid$(aob.Name, 2, 3) <> "Sys" Then     'Don't Export System Tables
  34.     DoCmd.TransferDatabase acExport, "Microsoft Access", strAbsoluteBkUpPath, _
  35.                            acTable, aob.Name, aob.Name
  36.   End If
  37. Next
  38.  
  39. 'Export all Queries
  40. For Each aob In CurrentData.AllQueries
  41.   DoCmd.TransferDatabase acExport, "Microsoft Access", strAbsoluteBkUpPath, _
  42.                          acQuery, aob.Name, aob.Name
  43. Next
  44.  
  45. 'Export all Forms
  46. For Each aob In CurrentProject.AllForms
  47.   DoCmd.TransferDatabase acExport, "Microsoft Access", strAbsoluteBkUpPath, _
  48.                          acForm, aob.Name, aob.Name
  49. Next
  50.  
  51. 'Export all Reports
  52. For Each aob In CurrentProject.AllReports
  53.   DoCmd.TransferDatabase acExport, "Microsoft Access", strAbsoluteBkUpPath, _
  54.                          acReport, aob.Name, aob.Name
  55. Next
  56.  
  57. 'Export all Macros
  58. For Each aob In CurrentProject.AllMacros
  59.   DoCmd.TransferDatabase acExport, "Microsoft Access", strAbsoluteBkUpPath, _
  60.                          acMacro, aob.Name, aob.Name
  61. Next
  62.  
  63. 'Export all Modules
  64. For Each aob In CurrentProject.AllModules
  65.   DoCmd.TransferDatabase acExport, "Microsoft Access", strAbsoluteBkUpPath, _
  66.                          acModule, aob.Name, aob.Name
  67. Next
  68.  
  69. DoCmd.Hourglass False
  70. End Function
P.S. - Just realized that you are using Access 2007. In that case, change the File Extension in Code Lines 12 (Comment), 13, and 16 to .accdb.

9 11138
ADezii
8,834 Expert 8TB
You may run into problems attempting to Backup the Current Database, so your best option may be to create a New Database with the Date appended to the Name, then export selectrive Objects to it.
Feb 4 '10 #2
Hi ADezzi, you are right, I already ran into problems, but I found few webpages where people have said they found the way... using APIs, modules, functions and macros... but was not published the steps and the final coding... I tried for many hours all the "intermediate" processes posted without lack. What makes me holding faith is "tools, database utilities, and backup database" do the magic, if the Access 2003 has those 3 clicks, should be some way to replicate the same.
Following your line of thoughts, do you have the code behind the button to export/import all the objects from a database?
Take care
Feb 4 '10 #3
ADezii
8,834 Expert 8TB
The code would be considerable, but when I get a chance I'll post a Template that you can work from.
Feb 4 '10 #4
OK, Thanks. I'll wait for it
Feb 4 '10 #5
ADezii
8,834 Expert 8TB
The following code will Backup all Database Objects to a Folder specified by the Constant conPATH_TO_BKUPS (Line #3). You can change this if you wish, but be sure to add the Trailing Backslash since I did not test for this for the sake of brevity. The actual Back Up Database will be the Base Name of the Current Database minus the .mdb Extension, plus an Underscore (_), plus the Date formatted as mmddyyyy, plus .mdb. Each Backup will be unique only as far as a single Day is concerned and will DELETE a previous Backup for that Day should it exist. I intentionally placed the code in a Public Function where it can easily and directly be called from a Main Switchboard Item. Have fun, and if you have any questions, please feel free to ask.
Expand|Select|Wrap|Line Numbers
  1. Public Function fExportAllDBObjects()
  2. On Error Resume Next
  3. Const conPATH_TO_BKUPS As String = "C:\DB Backups\"
  4. Dim strAbsoluteBkUpPath As String
  5. Dim aob As AccessObject
  6. Dim strDBName As String
  7. Dim wrkSpace As Workspace
  8. Dim dbBackup As Database
  9.  
  10. DoCmd.Hourglass True
  11.  
  12. 'Retrive the Current Database Name only, strip out .mdb
  13. strDBName = Replace(Mid$(CurrentDb.Name, InStrRev(CurrentDb.Name, "\") + 1), ".mdb", "")
  14.  
  15. 'Make the Backup DB Name unique for each Date
  16. strDBName = strDBName & "_" & Format$(Date, "mmddyyyy") & ".mdb"
  17.  
  18. strAbsoluteBkUpPath = conPATH_TO_BKUPS & strDBName
  19.  
  20. 'If a Bacup already exists for this Date, then DELETTE it
  21. If Dir$(strAbsoluteBkUpPath) <> "" Then
  22.   Kill strAbsoluteBkUpPath
  23. End If
  24.  
  25. 'Get Default Workspace.
  26. Set wrkSpace = DBEngine.Workspaces(0)
  27.  
  28. 'Create the Database
  29. Set dbBackup = wrkSpace.CreateDatabase(strAbsoluteBkUpPath, dbLangGeneral)
  30.  
  31. 'Export all Tables
  32. For Each aob In CurrentData.AllTables
  33.   If Mid$(aob.Name, 2, 3) <> "Sys" Then     'Don't Export System Tables
  34.     DoCmd.TransferDatabase acExport, "Microsoft Access", strAbsoluteBkUpPath, _
  35.                            acTable, aob.Name, aob.Name
  36.   End If
  37. Next
  38.  
  39. 'Export all Queries
  40. For Each aob In CurrentData.AllQueries
  41.   DoCmd.TransferDatabase acExport, "Microsoft Access", strAbsoluteBkUpPath, _
  42.                          acQuery, aob.Name, aob.Name
  43. Next
  44.  
  45. 'Export all Forms
  46. For Each aob In CurrentProject.AllForms
  47.   DoCmd.TransferDatabase acExport, "Microsoft Access", strAbsoluteBkUpPath, _
  48.                          acForm, aob.Name, aob.Name
  49. Next
  50.  
  51. 'Export all Reports
  52. For Each aob In CurrentProject.AllReports
  53.   DoCmd.TransferDatabase acExport, "Microsoft Access", strAbsoluteBkUpPath, _
  54.                          acReport, aob.Name, aob.Name
  55. Next
  56.  
  57. 'Export all Macros
  58. For Each aob In CurrentProject.AllMacros
  59.   DoCmd.TransferDatabase acExport, "Microsoft Access", strAbsoluteBkUpPath, _
  60.                          acMacro, aob.Name, aob.Name
  61. Next
  62.  
  63. 'Export all Modules
  64. For Each aob In CurrentProject.AllModules
  65.   DoCmd.TransferDatabase acExport, "Microsoft Access", strAbsoluteBkUpPath, _
  66.                          acModule, aob.Name, aob.Name
  67. Next
  68.  
  69. DoCmd.Hourglass False
  70. End Function
P.S. - Just realized that you are using Access 2007. In that case, change the File Extension in Code Lines 12 (Comment), 13, and 16 to .accdb.
Feb 4 '10 #6
Hi ADezzi,
Many Thanks!!!!
The code you posted works splendid!!!!
Thanks again!
Juan
Feb 5 '10 #7
ADezii
8,834 Expert 8TB
You are quite welcome, Juan.
Feb 5 '10 #8
neelsfer
547 512MB
This backup works very well. I adapted it to remove the date feature. How would one restore the same file it created from ie an external drive and replace the existing file?
I changed the code as shown below to export data to my e:drive
Expand|Select|Wrap|Line Numbers
  1. On Error Resume Next
  2.     Const conPATH_TO_BKUPS As String = "e:\"
  3.     Dim strAbsoluteBkUpPath As String
  4.     Dim aob As AccessObject
  5.     Dim strDBName As String
  6.     Dim wrkSpace As Workspace
  7.     Dim dbBackup As Database
  8.  
  9.     DoCmd.Hourglass True
  10.  
  11.     'Retrive the Current Database Name only, strip out .mdb
  12.     strDBName = Replace(Mid$(CurrentDb.Name, InStrRev(CurrentDb.Name, "\") + 1), ".accdb", "")
  13.  
  14.     'Make the Backup DB Name unique for each Date
  15.     strDBName = strDBName & ".accdb"
  16.      '& "_" & Format$(Date, "mmddyyyy")
  17.     strAbsoluteBkUpPath = conPATH_TO_BKUPS & strDBName
  18.  
  19.     'If a Bacup already exists for this Date, then DELETTE it
  20.     If Dir$(strAbsoluteBkUpPath) <> "" Then
  21.       Kill strAbsoluteBkUpPath
  22.     End If
  23.  
  24.     'Get Default Workspace.
  25.     Set wrkSpace = DBEngine.Workspaces(0)
  26.  
  27.     'Create the Database
  28.     Set dbBackup = wrkSpace.CreateDatabase(strAbsoluteBkUpPath, dbLangGeneral)
  29.  
  30.     'Export all Tables
  31.     For Each aob In CurrentData.AllTables
  32.       If Mid$(aob.Name, 2, 3) <> "Sys" Then     'Don't Export System Tables
  33.         DoCmd.TransferDatabase acExport, "Microsoft Access", strAbsoluteBkUpPath, _
  34.                                acTable, aob.Name, aob.Name
  35.       End If
  36.     Next
  37.  
  38.  
  39.  
  40.     DoCmd.Hourglass False
Jun 16 '11 #9
ADezii
8,834 Expert 8TB
  1. Make a Backup Copy of your Data and other Objects.
  2. DELETE Slective Objects from the Current Database.
  3. Import Selective Objects from the External Database to replace those Deleted from the Current Database.
  4. Should any part of this process fail, aside from the Backup, you will still have the Backup to return to.
Jun 19 '11 #10

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

Similar topics

4
by: Neil | last post by:
Hi, I hope this question isn't too far off topic....I'm almost at my wits end trying to figure this out. I have a Mysql database and I wish to automate the backup of the database because I...
5
by: TThai | last post by:
Hi, Appreciated anyone who can help us with our problem. Our backup job for ROUTT database has been failing for the last 4 days. It was working fine for almost a year now. The only thing...
6
by: Charles Morrall | last post by:
I have no experience with DB2 as such, but I've been tasked with configuring backup of a server running DB2 v8 on Windows Server 2003. I do have some experience with backups in general though. The...
1
by: Skully Matjas | last post by:
Is there a wat to make a command button for a backup of the database to floppy discs. 1) I would rather just back up the information I have in the Tables. Because that way it could fit onto...
6
by: Richard Hollenbeck | last post by:
I'd like a button on my main form to backup the database. How can I call up the Windows folder browser to prompt me for a filename and type (*.mdb) and folder to save in? I looked in my book and...
6
by: scottyman | last post by:
I can't make this script work properly. I've gone as far as I can with it and the rest is out of my ability. I can do some html editing but I'm lost in the Java world. The script at the bottom of...
12
by: alanchinese | last post by:
i am a db2 newbie. we have a server hosting a db2/6000 database that restricts the use of backup, generate ddl. i wonder if there is a simple way to transfer the database structure and data into my...
24
Dököll
by: Dököll | last post by:
Jumped for joy last night after learning, indeed, the command button wizard can be switched on or off. Well this occured here, with my Access 2000...an attempt to redo at work failed. Here's the...
1
by: ARC | last post by:
Does anyone know if you can add your own items to the round office button in the top left? I'm guessing the answer is no, and that you can only hide the default items, but it would be great if you...
5
by: Tony | last post by:
I am continuing to develop an Access 2007 application which was originally converted from Access 2003. In Access 2003 I was able to disable the Access Close button in the top righthand corner of...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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
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,...

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.