当找不到我的MS Access数据库时,我遇到了突然的程序崩溃。有人可以通过我的连接字符串帮助我。
这是代码:
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
答案 0 :(得分: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 :(得分: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"