BigDaveStafford
asked on
Permission Denied error 70 when using filecopy
I have a split database and wish to make a copy of the data back-end via code. To do this, I have a button on a form which calls the copy routine. One of the first actions in the code is to close the form which called the routine to ensure any remaining unwritten data is written to the database.
When I step through the code using F8 the routine works perfectly. When I run the code from the button on the form, I get an Error 70 Permission Denied error on the FileCopy line. I suspect that the backend database is still locked. but I don't know how I can get around this problem.
I've tried putting in a loop that continues to try the filecopy routine until the Error 70 doesn't occur. However, this doesn't seem to work as the routine gets trapped in the loop.
Is there a way to test if the Error 70 will occur before attempting it? Maybe testing to see if the back-end database is still locked?
Here's the code:
=============
Sub CreateBackUp()
On Error GoTo ErrTrap
'close any open forms and present the backup dialog
DoCmd.Close acForm, "frmContacts"
'grab the current backend name to rename the copy when its made
Dim myLinkedName
myLinkedName = CurrentDb.TableDefs("tblCo ntacts").C onnect
myLinkedName = Right(myLinkedName, Len(myLinkedName) - InStr(1, myLinkedName, "="))
'make the copy
FileCopy Source:=myLinkedName, destination:=Left(myLinked Name, Len(myLinkedName) - 6) & " " & Format(Now(), "yyyymmddhhss") & ".dcsdb"
'reopen the contacts form
DoCmd.OpenForm "frmContacts"
'avoid the error trap
Exit Sub
ErrTrap:
MsgBox "An unexpected error occured and is described as: " & Err.Description
DoCmd.OpenForm "frmContacts"
Exit Sub
End Sub
=================
Many thanks for your help.
When I step through the code using F8 the routine works perfectly. When I run the code from the button on the form, I get an Error 70 Permission Denied error on the FileCopy line. I suspect that the backend database is still locked. but I don't know how I can get around this problem.
I've tried putting in a loop that continues to try the filecopy routine until the Error 70 doesn't occur. However, this doesn't seem to work as the routine gets trapped in the loop.
Is there a way to test if the Error 70 will occur before attempting it? Maybe testing to see if the back-end database is still locked?
Here's the code:
=============
Sub CreateBackUp()
On Error GoTo ErrTrap
'close any open forms and present the backup dialog
DoCmd.Close acForm, "frmContacts"
'grab the current backend name to rename the copy when its made
Dim myLinkedName
myLinkedName = CurrentDb.TableDefs("tblCo
myLinkedName = Right(myLinkedName, Len(myLinkedName) - InStr(1, myLinkedName, "="))
'make the copy
FileCopy Source:=myLinkedName, destination:=Left(myLinked
'reopen the contacts form
DoCmd.OpenForm "frmContacts"
'avoid the error trap
Exit Sub
ErrTrap:
MsgBox "An unexpected error occured and is described as: " & Err.Description
DoCmd.OpenForm "frmContacts"
Exit Sub
End Sub
=================
Many thanks for your help.
ASKER
Thanks for this comment. If I use the File System Object can I be sure that any data from the form has been written to the backend file?
Thanks
Thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks again. I'll give the fso a try. I'm running the code from the front end of a split database so ensuring there's no pending edits should be easy.
Thanks
Thanks
ok
LOTS of cool things you can do with FSO ... pretty easy to use also.
thx.mx
thx.mx
Here are some links to get you started:
http://msdn.microsoft.com/en-us/library/aa242124%28VS.60%29.aspx
http://msdn.microsoft.com/en-us/library/hww8txat%28VS.85%29.aspx
http://msdn.microsoft.com/en-us/library/z9ty6h50.aspx
mx