2013-10-30 1 views
1

Excel 워크 시트의 데이터를 공유 Access 데이터베이스 (Access 2010)에 추가하는 매크로를 만들었습니다.VBA-Excel 매크로가 내 액세스 DB 잠그기 유지

매크로가 실행되면 셀 값을 가져와 Access 테이블의 단일 행으로 추가합니다. 여러 번 테스트 해봤는데 데이터를 추가하는 작업이 훌륭합니다.

매크로가 실행되면 문제가 발생합니다. 데이터베이스를 클릭하면 즉시 잠기고 데이터베이스를 열지 못하게됩니다. 이 문제를 해결하는 유일한 방법은 VBA에 들어가서 재설정 버튼을 누르는 것입니다. 어떤 이유로 데이터베이스를 잠금 해제합니다.

나는 Access 데이터베이스에 들어가서 Options> Client Settings를 No Locks로 설정했습니다.

아이디어를 잠그는 방법을 알려주세요. 닫기 메서드가 연결을 닫고 DB를 릴리스하지 않는 이유는 무엇입니까?

Dim Db As Database 
Dim Rs As Recordset 
Dim ws As DAO.Workspace 

Dim Path As String 
Path = "X:\EKTT-Log.accdb" 

Set ws = DBEngine.Workspaces(0) 

Set Db = ws.OpenDatabase(Path, _ 
False, False, "MS Access;") ' Learn more http://msdn.microsoft.com/en-us/library/office/ff835343.aspx 

Set Rs = Db.OpenRecordset("Results Log", dbOpenTable, dbAppendOnly, dbPessimistic) ' Learn more http://msdn.microsoft.com/en-us/library/office/ff820966(v=office.14).aspx 

' Log At a Glance 
If Sheets(">>>>").Cells(15, "G") <> "" Then 

Rs.AddNew 
Rs.Fields("CTYHOCN") = CTYHOCN 
Rs.Fields("eCommerce Manager") = eComMgr 
Rs.Fields("Timestamp Start") = TimeStart 
Rs.Fields("Timestamp Finish") = TimeFinish 
Rs.Fields("Global Web Page") = Sheets(">>>>").Cells(15, "B") 
Rs.Fields("Keyword Target") = Sheets(">>>>").Cells(15, "G") 
Rs.Fields("Est Search Vol") = Sheets(">>>>").Cells(15, "H") 
Rs.Fields("Title Tag") = Sheets(">>>>").Cells(15, "C") 
Rs.Fields("Meta Description") = Sheets(">>>>").Cells(15, "E") 
Rs.Update 


Else 
' 
End If 

' Close database & resume screenupdating 
Rs.Close 
Db.Close 
ws.Close 

Set Rs = Nothing 
Set Db = Nothing 
Set ws = Nothing 

Application.ScreenUpdating = True 

답변

0

여기 경우 다른 사람이 우리의 솔루션은 유사한 문제를 가지고 있습니다 : 여기

내가 잠시 전에이 작업을 수행하는 방법을 자세히 쓴 답변입니다.

참조 : http://msdn.microsoft.com/en-us/office/bb208861 & http://msdn.microsoft.com/en-us/library/dd627355(v=office.12).aspx

Sub DataImport() 

' Declare datbase variables 
Dim DatabasePath As String 
Dim dbs As Database 

' Provide database path 
DatabasePath = "C:\database.accdb" 

' Open database connection 
Set dbs = OpenDatabase(DatabasePath) 

' Get values 
GlobalWebPage = Sheets(">>>>").Cells(15, "B") 
KeywordTarget = Sheets(">>>>").Cells(15, "G") 
EstSearchVol = Sheets(">>>>").Cells(15, "H") 
TitleTag = Sheets(">>>>").Cells(15, "C") 
MetaDescription = Sheets(">>>>").Cells(15, "E") 

' Escape characters before SQL statement 
GlobalWebPage = FixQuote(GlobalWebPage) 
KeywordTarget = FixQuote(KeywordTarget) 
EstSearchVol = FixQuote(EstSearchVol) 
TitleTag = FixQuote(TitleTag) 
MetaDescription = FixQuote(MetaDescription) 

' Execute SQL statement 
dbs.Execute " INSERT INTO ResultsLog " _ 
     & "(CTYHOCN, eCommerceManager, TimestampStart, TimestampFinish, GlobalWebPage, KeywordTarget, EstSearchVol, TitleTag, MetaDescription) VALUES " _ 
     & "('" & CTYHOCN & "', '" & eComMgr & "', '" & TimeStart & "', '" & TimeFinish & "', '" & GlobalWebPage & "', '" & KeywordTarget & "', '" & EstSearchVol & "', '" & TitleTag & "', '" & MetaDescription & "');" 

' Close the database connection 
dbs.Close 

End Sub 


' Function courtesy of http://mikeperris.com/access/escaping-quotes-Access-VBA-SQL.html 
Public Function FixQuote(FQText As String) As String 
On Error GoTo Err_FixQuote 
FixQuote = Replace(FQText, "'", "''") 
FixQuote = Replace(FixQuote, """", """""") 
Exit_FixQuote: 
Exit Function 
Err_FixQuote: 
MsgBox Err.Description, , "Error in Function Fix_Quotes.FixQuote" 
Resume Exit_FixQuote 
Resume 0 '.FOR TROUBLESHOOTING 
End Function