2014-12-12 2 views
0

DB 붙여 넣기를 복사하고 몇 가지 항목을 변경하려고했습니다.연결이 닫힌 후 DB를 복사 할 때 사용 권한이 거부되었습니다.

나는 거의 내가 거기에 있다고 생각하지만 문제가 발생했습니다.

내가 연결할 때까지 db를 아무 문제없이 복사 할 수있는 것처럼 보입니다. 연결 중 (의미가 있음) 또는 연결을 닫은 후에 더 이상 DB를 복사하여 붙여 넣을 수 없습니다.

나는 거기에 연결 된 (하지만 지금은 닫힌) db 어딘가에 내 코드에서 내 첫 번째 코드를 통해 실행에 대한 유령 참조가 있는지 확신 해요 두 번째 복사본에 붙여 넣기하지만 그 후에 오류가 첫 번째 복사본으로 이동하여 붙여 넣습니다. 그것이 접근을 닫고 다시 열 때까지입니다. 이 시점에서 오류는 두 번째 복사본으로 되돌아 와서 붙여 넣습니다.

나는 이것이 누군가에게 조금 의미가 있으며 올바른 방향으로 나를 가리킬 수 있기를 바랍니다.

질문 : 연결을 완료 한 후에 연결에 대한 모든 참조를 지우려면 어떻게해야합니까?

코드 샘플이 질문을 읽고 깊이 생각하는 시간을내어

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 

감사합니다.

편집 : 오류가 발생한 줄을 언급 한 적이 없다는 것을 눈치 채 셨습니다. 오류가 나는 라인

dbFile = fso.CopyFile(oriLoc, destLoc, True) 

에 하위 DuplicateDB 전화를 두 번째로 발생하는 에러는 런타임되는 오류 '70': 사용 권한이

con.Open _ 
    "Driver={Microsoft Access Driver (*.mdb, *.accdb)};" & _ 
    "Dbq=" & Loc & _ 
    "Exclusive=1;" & _ 
    "Uid=admin;" & _ 
    "Pwd=;" 
에서 내 con.open을 변경
+0

연결 개체의 범위 (temp)를 보면 Transfer() 하위에 선언되고 설정되므로 이상적으로 해당 하위 끝에서 닫혀 있어야합니다. 다른 연결 개체와 동일합니다. 나는 그것이 선언되지 않았기 때문에 clearsensitivedata에서 그것을 닫지 않을 것이고, 당신은 clearsensitivedata 후에 그것을 계속 사용하고 싶을 것입니다. 전송 서브의 끝 전에 그들을 모두 닫으십시오. – CustodianOfCode

+0

방금 ​​연결 코드를 ClearSensitiveData 내부로 이동하여 동일한 하위에서 열거 나 닫을 수있게하려고했으나 (다른 연결을 잠시 없앴습니다) 여전히 동일한 오류가 발생합니다. 일단 연결을 닫더라도 DB 파일에 복사/이동하지 못하게하는 것 같습니다. – Tolure

+0

나는 연결을 닫은 직후에 con = nothing 을 추가하려고 시도했지만 문제가 해결되지 않았습니다. – Tolure

답변

0

거부

to

con.Open _ 
     "Driver={Microsoft Access Driver (*.mdb, *.accdb)};" & _ 
     "Dbq=" & Loc & ";" 

이제는 작동하는 것 같습니다.