473,385 Members | 1,523 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,385 software developers and data experts.

Backup database via VBA

15
Is there a way to backup an Access database to another folder and rename the backup using VBA code?
Apr 16 '09 #1
6 15732
NeoPa
32,556 Expert Mod 16PB
You can Shell() to a COPY command (or a CMD file which includes a COPY). I would advise using ShellWait() though, as continuing to process a database while it is being copied is a recipe for corruption. Of the copy database at least.
Apr 16 '09 #2
ADezii
8,834 Expert 8TB
@RachH
Hvae you considered something as simple as a Batch File, to which a Short Cut can exist on your Desktop?
Apr 16 '09 #3
Stewart Ross
2,545 Expert Mod 2GB
Hi. In one of my applications I have an Access DB which is set via the Windows task scheduler to open at 5am and close itself automatically when done. One of the tasks it performs is to copy a master database to a backup location. It renames the previous backup copy of the master DB then copies in the master DB.

The code uses the filesystem object to do the copying and renaming and is listed below. It refers to four form controls which are bound to fields in an underlying table that record the master url for the database being copied, the copy url for the location it is being copied to, and the file names (with extensions) of the original and copied files.

I provide this as a somewhat rough and ready routine, which for me is in use every day to provide an off-line copy of the master database on which I can work without danger of damaging a live application. In my case the database copied records the details of all incidents attended by the Fire and Rescue Service I work for, so working on the live data is too big a risk.

Expand|Select|Wrap|Line Numbers
  1. Private Sub CopyDB_Click()
  2.     Dim fso As Object, f2 As Object, f3 As Object
  3.     Dim strCopyFrom As String, strCopyTo As String, strPrev As String
  4.     Dim strPrevLoc As String
  5.     On Error Resume Next
  6.     Set fso = CreateObject("Scripting.FileSystemObject")
  7.     strCopyFrom = Me.Control_DB_Original_URL & "\" & Me.Control_DB_Name
  8.     strCopyTo = Me.Control_DB_Copy_URL & "\" & Me.Copy_DB_Name
  9.     strPrev = "Prev-" & Me.Copy_DB_Name
  10.     strPrevLoc = Me.Control_DB_Copy_URL & "\" & strPrev
  11.     Me.Message = "Copy started at " & Format(Time(), "hh:mm") & " hrs." & vbCrLf & "This can take around 10 mins to complete..."
  12.     Me.Repaint
  13.     DoCmd.Hourglass True
  14.     fso.DeleteFile strPrevLoc ' delete the previous backup version of the file.
  15.     Set f3 = fso.Getfile(strCopyTo)
  16.     f3.Name = strPrev
  17.     On Error GoTo errhandler
  18.     Set f2 = fso.Getfile(strCopyFrom)
  19.     f2.Copy (strCopyTo)
  20.     Me.Message = Me.Message & vbCrLf & "Copy ended at " & Format(Time(), "hh:mm") & " hrs."
  21.     Me.Repaint
  22.     Set fso = Nothing
  23.     DoCmd.Hourglass False
  24.     DoCmd.Close acForm, Me.Name
  25.     Exit Sub
  26. errhandler:
  27.     MsgBox "Error: " & Err.Number & " -> " & Err.Description
  28.     DoCmd.Hourglass False
  29. End Sub
-Stewart
Apr 17 '09 #4
NeoPa
32,556 Expert Mod 16PB
The reason I opted for the COPY command Stewart, is that it can work even on an open database (I was thinking the OP probably wanted the db to back itself up).

If that's not an issue then the FileSystem Object approach as you have it is flexible and does the job :)
Apr 17 '09 #5
RachH
15
Wow thanks for all the great responses! I'll play with things a bit to figure out what will work best for our need. I really appreciate the help :-)
Apr 17 '09 #6
NeoPa
32,556 Expert Mod 16PB
You're welcome Rach :)

Welcome to Bytes!
Apr 18 '09 #7

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...
1
by: Andrew E | last post by:
I just took over DBA responsibilities for an Oracle 8i database running on Linux. Although I've been working with relational databases for some time, I'm a bit green on Oracle so forgive me. My...
3
by: Thiko | last post by:
Hi I take one nightly full database backup at 02:00 and backup the transaction log to one backup set every 15mins. The commands to do this are as follows and are set up to run as database...
2
by: nt | last post by:
I am having a problem with a regular backup of an SQL Server (MSDE 2000) database to a local drive. I initiate the backup once a week, by issuing the required T-SQL, via ADO. In this case, the...
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...
1
by: alex | last post by:
Hi ! I couldn't make backups with our new system using db2 8.2. Every time I trigger a backup I get this error message: BACKUP DATABASE EBUERO2 ONLINE TO "/raid/backup/ebuero2/part1",...
4
by: uthuras | last post by:
Hi all, I have DB2ESE version 8.1 with FP 4 on AIX 5.2. My database used to be 1.1TB. When the DB size is 1.1TB, it takes approximately 7 hours to backup the entire database (online backup). The...
5
by: jag | last post by:
hi, I am thinking about backup strategy. I have couple of questions for my db2 community brothers. 1. Can i use full online or offline database backup as base for incremental backup for...
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......
0
by: Massimiliano Campagnoli | last post by:
I am trying to backup sample database to network drive J:\ which maps to a SAMBA share. The first time the backup is successfull but the the next time fails. I've to delete J:\sample.0 if I want...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.