2014-12-16 2 views
1

MS Access 데이터베이스를 찾을 수 없을 때 갑자기 프로그램이 중단되었습니다. 누군가 나를 연결 고리로 도울 수 있습니까? 여기

코드의 : 실패한 연결을 감지하기 위해
connectionstring 데이터베이스 연결 확인

 
    DoEvents 
    Set con = New ADODB.Connection 
    With con 
    .ConnectionString = "Driver={Microsoft Access Driver (*.mdb)};Dbq=mydatabase.mdb;DefaultDir=C:\Projects\Database\;Uid=;Pwd=" 
    .CursorLocation = adUseClient 
    .Open 
    End With 


나는이 같은 코드를 넣어 뭔가 할 ...

 
    if connection = successful then 
     continue to table query... 
    else 
     show message box 
    endif 


답변

1

, 당신 오류 트래핑을해야합니다.

' Start error trapping. 
On Error Resume Next 

Set con = New ADODB.Connection 
With con 
    .ConnectionString = "Driver={Microsoft Access Driver (*.mdb)};Dbq=mydatabase.mdb;DefaultDir=C:\Projects\Database\;Uid=;Pwd=" 
    .CursorLocation = adUseClient 
    .Open 
End With 

' Check for error. 
If Err <> 0 Then 
    ' Error. 
    Msgbox("Error during connection.") 
Else 
    ' Success. 
End If 

' End error trapping. 
On Error GoTo 0 
1

이 같은 오류 처리를 사용할 수 있습니다

On Error Goto ConErr 

Set con = New ADODB.Connection 
With con 
.ConnectionString = "Driver={Microsoft Access Driver (*.mdb)};Dbq=mydatabase.mdb;DefaultDir=C:\Projects\Database\;Uid=;Pwd=" 
.CursorLocation = adUseClient 
.Open 
End With 

' here insert query code or Goto statemen ' 

Exit sub 
ConErr: 
MsgBox "connection error" 
관련 문제