问题
I've been trying to copy paste a db and change a few entries.
I believe I'm almost there but I hit a problem.
It seems that I can copy my db no problem until I connect with it. During my connection (which makes sense) or after I close the connection I am no longer able to copy and paste the DB as I did before.
I'm pretty sure there is a phantom reference to the connected (but now closed) db remaining somewhere in my code for on my first run through of the code I get the error on the second copy and paste but after that the error jumps up to the first copy and paste. That is until I close access and reopen it. At that point the error returns to the second copy and paste.
I hope this makes a little sense to someone and they can point me in the right direction.
Question: How do I clear all reference to a connection after I'm done with it.
Code Sample
Sub Transfer()
'Connection related to DB that contains the master data
Dim ori As Object
Set ori = CreateObject("ADODB.Connection")
Dim oriLoc As String
oriLoc = "Path\RAUMain.accdb"
'Connection related to DB that will host the data.
Dim dest As Object
Set dest = CreateObject("ADODB.Connection")
Dim destLoc As String
destLoc = "Path\Mirror DB\RAUMain.accdb"
'Location and connection that the DB will duplicated and stored until sensitive data is removed
'This is to prevent the case that an error in removing sensitive data would leave the db
'in a location where people would be able to see data that they are not suppose to.
Dim temp As Object
Set temp = CreateObject("ADODB.Connection")
Dim tempLoc As String
tempLoc = "Path\tempRAUMain.accdb"
'Duplicate ori DB
Call DuplicateDB(oriLoc, tempLoc)
'Clear all Sensitive Data from tempDB
'All entries that don't have the share field
' marked as true will be stripped of sensitive data
Call ClearSensitiveData(temp, tempLoc)
'Duplicate the temp data into the destination location
Call DuplicateDB(tempLoc, destLoc)
End Sub
'Duplicates the DB so to insure all data is accurate.
Sub DuplicateDB(oriLoc As String, destLoc As String)
Dim fso As Object
Set fso = VBA.CreateObject("Scripting.FileSystemObject")
dbFile = fso.CopyFile(oriLoc, destLoc, True)
End Sub
'Duplicates the DB so to insure all data is accurate.
Sub DuplicateDB(oriLoc As String, destLoc As String)
Dim fso As Object
Set fso = VBA.CreateObject("Scripting.FileSystemObject")
dbFile = fso.CopyFile(oriLoc, destLoc, True)
End Sub
Sub ClearSensitiveData(ByRef con As Object, dbLoc As String)
'Connect to the DB
Call Connect(con, dbLoc)
'Code Clear sensitive data
con.Close
'con.Quit
End Sub
Sub Connect(ByRef con As Object, ByVal Loc As String)
Loc = Loc & ";"
On Error GoTo UpdatePublicDatabase_OpenError
con.Open _
"Driver={Microsoft Access Driver (*.mdb, *.accdb)};" & _
"Dbq=" & Loc & _
"Exclusive=1;" & _
"Uid=admin;" & _
"Pwd=;"
On Error GoTo 0
Debug.Print "Done."
Exit Sub
UpdatePublicDatabase_OpenError:
Debug.Print "Exclusive 'Open' failed. Quitting."
Exit Sub
End Sub
Thanks for taking the time to read and ponder this question.
EDIT: I just noticed that I never mentioned on which line the error occurs. The error happens the second time I call Sub DuplicateDB on line
dbFile = fso.CopyFile(oriLoc, destLoc, True)
and the error is Run-time error '70': Permission Denied
回答1:
Change my con.open from
con.Open _
"Driver={Microsoft Access Driver (*.mdb, *.accdb)};" & _
"Dbq=" & Loc & _
"Exclusive=1;" & _
"Uid=admin;" & _
"Pwd=;"
to
con.Open _
"Driver={Microsoft Access Driver (*.mdb, *.accdb)};" & _
"Dbq=" & Loc & ";"
And it seems to work now.
来源:https://stackoverflow.com/questions/27436898/permission-denied-in-copying-db-after-connection-closed