Backup code getting "Permission denied" error (1 Viewer)

OBBurton

Registered User.
Local time
Yesterday, 22:44
Joined
Dec 26, 2013
Messages
77
Hi,

I got these three code routines for backing up an Access database file from two different forums. I installed them as is, on a test database, creating command buttons for each. The test database is not password protected. I intend (if I can get one of these working) to install it on simple, single file, password protected databases.

The first two came from Access World Forums at: http://www.access-programmers.co.uk/forums/showthread.php?t=226520

Code:
Function BACKUPS()
On Error GoTo Err_BACKUPS
Dim fso As Object
Dim sSourcePath As String
Dim sSourceFile As String
Dim sBackupPath As String
Dim sBackupFile As String
'Dim db As New Access.Application
Dim buf As String
Dim strBu As String
sSourceFile = CurrentProject.Name
sSourcePath = CurrentProject.Path & "\"
sBackupFile = CurrentProject.Name
sBackupPath = CurrentProject.Path & "\Backups\"
Set fso = CreateObject("Scripting.FileSystemObject")
fso.CopyFile sSourcePath & sSourceFile, sBackupPath & sBackupFile, True
Set fso = Nothing
Beep
MsgBox "Backup was successful and saved @ " & Chr(13) & Chr(13) & sBackupPath & Chr(13) & Chr(13) & "The backup file name is " & Chr(13) & Chr(13) & sBackupFile, vbInformation, "Backup Completed"
Exit_BACKUPS:
Exit Function
Err_BACKUPS:
MsgBox Err.Number & Err.Description
Resume Exit_BACKUPS
End Function
I got a compile error "Invalid use of property." on the button sub. I found this code rather confusing.

Then there was a simple On Click sub:
Code:
Private Sub Backup_Click()
Dim fs
Set fs = CreateObject("Scripting.FileSystemObject")
fs.CopyFile CurrentProject.FullName, "C:\", True
Set fs = Nothing
MsgBox "Database has been backed up successfully"
End Sub
Private Sub Command32_Click()
End Sub
On this one I got a "Permission denied." error on the CopyFile method. As I said before, my database is not password protected. This code seemed overly simplistic, but apparently worked fine for aussie and sk.shafiqul, just not for me.

I found this third bit of code on another forum: http://mybusinessdatabase.com/vba-code-to-backup-an-access-database/
Code:
Public Function db_Backup()
On Error GoTo db_Backup_Err

Dim sourceFile As String, destinationFile As String
Dim aFSO As Variant
Dim path As String, name As String

sourceFile = CurrentProject.FullName
path = CurrentProject.path
name = CurrentProject.name
destinationFile = path & "\db backups - please do not remove\" & _
    Left(name, Len(name) - 6) & "_backup" & "_" & _
    Year(Now) & "_" & Month(Now) & "_" & Day(Now) & ".accdb"

'this removes a file created on the same day
If Dir(destinationFile) <> "" Then
    Kill destinationFile
End If

'this creates a backup into destination path
If Dir(destinationFile) = "" Then
    Set aFSO = CreateObject("Scripting.FileSystemObject")
    aFSO.CopyFile sourceFile, destinationFile, True
    MsgBox "A database backup has been stored under " & destinationFile
End If

db_Backup_Exit:
    Exit Function
    
db_Backup_Err:
    MsgBox ("Error in db_Backup function" & vbCrLf & _
        "Error: #" & Err.Number & vbCrLf & _
        "Description: " & Err.Description)
    Resume db_Backup_Exit
    
End Function
On this one I got another "Permission denied." error on the CopyFile method. Again, my database is not password protected. This code had appeared to me to be the most promising of the three.

Can anyone tell me which of these is the most likely to work without totally rewriting it and why these errors might be happening? Any help would be greatly appreciated.
 
Last edited:

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 06:44
Joined
Jul 9, 2003
Messages
16,364
The folder where you are sending the backup does it exist?
 

OBBurton

Registered User.
Local time
Yesterday, 22:44
Joined
Dec 26, 2013
Messages
77
Thanks for responding, Uncle Gizmo!
I had thought of that and added the folder after I last posted. I created the folder in great hopes that it would now work, but alas, it was not to be. I even changed the name to "Backups" thinking that the spaces might be causing the problem, but it was still no go. Do you have any other thoughts?
 

spikepl

Eledittingent Beliped
Local time
Today, 07:44
Joined
Nov 3, 2010
Messages
6,142
Which windows? Make a subfolder , because Win is no longer happy about people dumping stuff in C:
 

OBBurton

Registered User.
Local time
Yesterday, 22:44
Joined
Dec 26, 2013
Messages
77
Thanks for responding, spikepl!
I do appreciate it, but I tried changing the path to an existing backups folder and still got a "Permission Denied" error.
 

OBBurton

Registered User.
Local time
Yesterday, 22:44
Joined
Dec 26, 2013
Messages
77
Thanks, Uncle Gizmo!
Originally it was on a Microsoft OneDrive folder, but I tried putting it on my local hard drive and still got the "Permission Denied." error. I am the administrator on the machine, but that and $4.00 will get you a nice cup of coffee at Starbucks, but not necessarily permission to copy a file! :D!
 

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 06:44
Joined
Jul 9, 2003
Messages
16,364
Well seeing as there are 3 samples and none work, I'm assuming that the problem is your machine or set-up, otherwise errors would have been reported by other users of the code.

I would suggest create a new DB and try it in that.

I'm also wondering if you need a particular reference to use "Scripting.FileSystemObject" ?
 

vbaInet

AWF VIP
Local time
Today, 06:44
Joined
Jan 22, 2010
Messages
26,374
When you're copying a file you need to give it the full path (plus the file name) of the destination. Not just the path to the directory.

E.g.
Code:
fso.CopyFile "C:\MainDirectory\Sub\Sub\MyDb.accdb", _
             "[COLOR="Blue"]C:\MainDirectory\Sub\Sub\MyDb_20140823.accdb[/COLOR]"
As you can see I'm using the full path. You will get a permission denied error if you don't use the full path.
 

OBBurton

Registered User.
Local time
Yesterday, 22:44
Joined
Dec 26, 2013
Messages
77
Thanks for responding vbaInet!
I have tried plugging in an exact path ("C:\Users\OBBurton\Documents\Backups\OLB_Products.accdb") and I still get the Permission Denied" error message.
 

vbaInet

AWF VIP
Local time
Today, 06:44
Joined
Jan 22, 2010
Messages
26,374
Then you probably have other users connected to the back end.
 

OBBurton

Registered User.
Local time
Yesterday, 22:44
Joined
Dec 26, 2013
Messages
77
Thanks again vbaInet!
Unfortunately, I don't have a split db, so I am the only user.
 

jdraw

Super Moderator
Staff member
Local time
Today, 01:44
Joined
Jan 23, 2006
Messages
15,394
I recall seeing this error before. I searched the forum and found
http://www.access-programmers.co.uk/forums/showthread.php?t=145128

There was note in the original code re Error 70.


"' Must set security properties of the mdb (inherit)
' else you'll get permission error #70"


It was 6+ yrs ago, but as I recall I had to look at properties and there was an option to "inherit".

Also note via Google there are many causes for Error 70 Permissions Denied
Good luck.
 

OBBurton

Registered User.
Local time
Yesterday, 22:44
Joined
Dec 26, 2013
Messages
77
Thanks, jdraw and Uncle Gizmo for your responses!
I did try a new DB and unfortunately it did not work. I have however found a rather painful solution. On a hunch, inspired by a post by jdraw, I tried the code from a different Windows profile. In short it worked! Now I just have to redo a lot of work I've done in my old profile.

Many thanks to jdraw, Uncle Gizmo, vbaInet and spikepl for your efforts to help me with this problem! :D
 

vbaInet

AWF VIP
Local time
Today, 06:44
Joined
Jan 22, 2010
Messages
26,374
Backups are best taken when no one is logged into the system and it only makes sense to make a backup of a back end when it's not in use. In your case your db is not split so Access is safeguarding your db by disallowing it from being copied. Access knows to safeguard your db because FileCopy is native to Access and FileSystemObject object is created from within Access and it's a COM object.

If your db was split you will be making a backup of the backend and because your backend is not open (it's only the front end that is open) it should copy successfully. If you're working on a non split db and you want to make a "risky" backup you would use Shell() and the Copy command which is instantiated from Shell and runs like a normal copy and paste procedure. Risky because you're not sure what you're copying, i.e. what is the baseline.

Also you backup the front end independently if necessary.
 

OBBurton

Registered User.
Local time
Yesterday, 22:44
Joined
Dec 26, 2013
Messages
77
Thanks, vbaInet for your response!

I read your post very carefully, but splitting my database isn't really an option for me unless you can direct me to instructions to do it manually. You are wrong about one thing though. Access does allow backinging up the database through the use of the second and third examples of code above. The reason I was receiving the "Permission Denied" error, was some unknown problem with my Windows profile. I logged on as a different user and both examples worked with some minor adjustments to the paths. I am not too concerned about my backups being bad since only one user at a time (my wife or I) will be using the databases. I am also creating a new dated file each time, so even if one backup is bad there will be others to draw from.
 
Last edited:

vbaInet

AWF VIP
Local time
Today, 06:44
Joined
Jan 22, 2010
Messages
26,374
Whether one or more person(s) is/are using the db it still should be split.
http://www.kallal.ca/Articles/split/index.htm

You are wrong about one thing though. Access does allow backinging up the database through the use of the second and third examples of code above. The reason I was receiving the error, was some unknown problem with my Windows profile.
Like I said it's a safeguard. I don't know why it would prevent one profile from copying and not the other. There are obviously some security permissions with one profile that the other doesn't have.
 

OBBurton

Registered User.
Local time
Yesterday, 22:44
Joined
Dec 26, 2013
Messages
77
Thanks, vbaInet, for your response and good advice!
But, as I said before, splitting the database is not an option for me. My wife misplaced our installation disk and the option for splitting is an uninstalled add-in.
 

Users who are viewing this thread

Top Bottom