473,473 Members | 1,754 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Backup database via VBA

15 New Member
Is there a way to backup an Access database to another folder and rename the backup using VBA code?
Apr 16 '09 #1
6 15739
NeoPa
32,556 Recognized Expert Moderator MVP
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 Recognized Expert Expert
@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 Recognized Expert Moderator Specialist
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 Recognized Expert Moderator MVP
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 New Member
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 Recognized Expert Moderator MVP
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: 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
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
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.