Permission Denied while trying to save a back-up copy through VBA code (1 Viewer)

SachAccess

Active member
Local time
Tomorrow, 04:16
Joined
Nov 22, 2021
Messages
389
Hi,
I am using below code to create back-up of my MS Access file.
I have changed the original names however, have checked the actual path name manually and it is accessible.
This code was written yesterday. It was working fine till one our ago. In fact I have created multiple back-ups files with this code.

I tried to change this code to function and get it run via AutoExec, it did not work for me due to some reasons.
Hence I reverted from Function to Sub. And thought that I will assign a VBA Sub to a button for this.
However, when am running the same code now, am getting an error at FileCopy SourceFile, DestinationFile saying permission denied.
I am not able to understand the reason. I have not changed anything in the code. It was working fine. Then why this error.
Can anyone please help me in this.

Code:
Sub BackUp()
Dim SourceFile, DestinationFile
Dim MyFileName As Variant
SourceFile = "M:\ MyActualPathHere \MyActualFileName.accdb" ' Define source file name.
MyFileName = Day(Now)
MyFileName = MyFileName & "-" & Month(Now)
MyFileName = MyFileName & "-" & Year(Now)
MyFileName = MyFileName & "-" & Hour(Now)
MyFileName = MyFileName & "-" & Minute(Now)
MyFileName = MyFileName & "-" & Second(Now)
MyFileName = "BackUpFile" & "-" & MyFileName & "-" & Trim(UCase(Environ("UserName"))) & ".accdb"
DestinationFile = "M:\MyActualPathHere\ " & MyFileName
FileCopy SourceFile, DestinationFile ' Copy source to target.
End Sub
 

Ranman256

Well-known member
Local time
Today, 18:46
Joined
Apr 9, 2015
Messages
4,337
i've had problems with FileCopy so use this instead:

DestFile = "m:\MyFileName.accdb_" & Format(Now(),"yymmdd-hhnn") & ".accdb"
Copy1File SrcFile, DestFile

Code:
Public Function Copy1File(ByVal pvSrc, ByVal pvTarg) As Boolean
Dim fso
On Error GoTo errMake

Set fso = CreateObject("Scripting.FileSystemObject")    '(reference: ms Scripting Runtime)
fso.CopyFile pvSrc, pvTarg
Copy1File = True
Set fso = Nothing
Exit Function

errMake:
'MsgBox Err.Description & vbCrLf & pvSrc, , "Copy1File(): " & Err
Set fso = Nothing
End Function
 

SachAccess

Active member
Local time
Tomorrow, 04:16
Joined
Nov 22, 2021
Messages
389
i've had problems with FileCopy so use this instead:

DestFile = "m:\MyFileName.accdb_" & Format(Now(),"yymmdd-hhnn") & ".accdb"
Copy1File SrcFile, DestFile

Code:
Public Function Copy1File(ByVal pvSrc, ByVal pvTarg) As Boolean
Dim fso
On Error GoTo errMake

Set fso = CreateObject("Scripting.FileSystemObject")    '(reference: ms Scripting Runtime)
fso.CopyFile pvSrc, pvTarg
Copy1File = True
Set fso = Nothing
Exit Function

errMake:
'MsgBox Err.Description & vbCrLf & pvSrc, , "Copy1File(): " & Err
Set fso = Nothing
End Function
Thanks a lot @Ranman256 , am checking this and will revert. Have a nice day ahead. :)
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 17:46
Joined
Feb 28, 2001
Messages
27,189
Just as a thought, and there is nothing wrong with what you are doing, but...

Code:
MyFileName = Day(Now)
MyFileName = MyFileName & "-" & Month(Now)
MyFileName = MyFileName & "-" & Year(Now)
MyFileName = MyFileName & "-" & Hour(Now)
MyFileName = MyFileName & "-" & Minute(Now)
MyFileName = MyFileName & "-" & Second(Now)

could be done as this:

Code:
MyFileName = Format( Now, "yyyy-mm-dd-hh-nn-ss" )

in which case the names would then sort in date order AND it would only take one line of code to achieve that string you are assembling. By putting the dates as year, month-number, day-number, you get automated calendar sorting within the folder.

Again, you are doing nothing wrong, but what you are doing is not as efficient as it could be.
 

SachAccess

Active member
Local time
Tomorrow, 04:16
Joined
Nov 22, 2021
Messages
389
Hi @The_Doc_Man thanks a lot for the help. I agree, mine is not efficient method.
I was trying with something similar to 'MyFileName = Format( Now, "yyyy-mm-dd-hh-nn-ss" )' only however I faced bug in my code.
So at that moment I thought to skip that part for the time being and try check if back-up is getting created.

Have a nice weekend. :)
 

Users who are viewing this thread

Top Bottom