2016-10-12 4 views
0

MS Access 2007/2010 데이터베이스에서 데이터를 추정하려고합니다.MS Access 데이터베이스의 연결 문자열이 잘못되었습니다.

VBA에 다음 코드가 있지만 연결 문자열이 잘못되었습니다. 관련 참조 라이브러리를 추가했습니다.

Private Sub btnGetMsAccessData_Click() 

Dim sConn As String 
Dim oConn As ADODB.Connection 
Dim oRs As ADODB.Recordset 
Dim sSQL As String 

sConn = "Provider=Microsoft.ACE.OLEDB.12.0;User ID=Admin;Data Source=\\MyNetworkPath\BP-MasterDashboard Source\BP_Planning_by_PT_dept_be.accdb;Mode=Read" 

Set oConn = New ADODB.Connection  ' Open a connection. 
oConn.Open 


sSQL = "SELECT * FROM Tbl_Start_Leaver"  ' Make a query over the connection. 
Set oRs = New ADODB.Recordset 
oRs.Open sSQL, , adOpenStatic, adLockBatchOptimistic, adCmdText 

MsgBox oRs.RecordCount 

oConn.Close ' Close the connection. 
Set oConn = Nothing 

End Sub 

oConn.Open 행에서 알 수없는 응용 프로그램 오류가 발생하지 않습니다.

통합 문서를 테이블 중 하나에 연결하려고 시도했지만 정상적으로 작동합니다. 그런 다음 "연결"을보고 내 코드로 복사했지만 여전히 기쁨은 없습니다. 자동화 오류 예기치 않은 오류 모든 아이디어를 주시면 감사하겠습니다

:

는 말을 유지합니다.

미리 감사드립니다.

답변

2

연결 문자열이 잘못되었지만 다른 문제도있었습니다. 연결 문자열을 ADODB Connection 객체뿐만 아니라 다른 객체에도 할당하지 않는다. 수정 된 코드는 다음과 같습니다.

Private Sub btnGetMsAccessData_Click() 
    'Ensure you add a reference to Microsoft ADO Objects 
    Dim oConn As New ADODB.Connection 
    Dim oRs As New ADODB.Recordset 
    Dim sSQL As String: sSQL = "SELECT * FROM Tbl_Start_Leaver" 
    'Corrected Connection String from Thomas Inzina 
    Dim sConn As String: sConn = "Provider=Microsoft.ACE.OLEDB.12.0;UID=Admin;Data Source=" & _ 
           "\\MyNetworkPath\BP-MasterDashboard Source\BP_Planning_by_PT_dept_be.accdb;Mode=Read" 

    With oConn 
     .ConnectionString = sConn ' You need to assign the connection string to the ADODB.Connection Object 
     .Open 
    End With 

    'Make sure the connection isn't open before opening the recordset 
    'You also need to specify which connection you want to use as the second parameter (this was missed) 
    If oRs.State <> adStateOpen Then oRs.Open sSQL, oConn, adOpenStatic, adLockBatchOptimistic, adCmdText 

    'Close Connection and RS 
    If oConn.State = adStateOpen Then oConn.Close 
    If oRs.State = adStateOpen Then oRs.Close 

    'Clean Up 
    Set oRs = Nothing 
    Set oConn = Nothing 
End Sub 
+0

이 코드는 작동하지 않습니다. 아마 내가 필요로하는 lib 참조를 생각하고 있습니다. – mond007

+0

코드 2 행에 대한 내 의견보기. ADO에 대한 참조가 필요합니다. 특히'Microsoft ActiveX Data Objects 2.X Library' 또는 버전 6.x를 사용할 수 있습니다. 어느 쪽이든 작동해야합니다. –

+0

Microsoft ActiveX Data Objects 6.1 라이브러리가 있습니다. 문제는 아닙니다. – mond007

관련 문제