on error resume next:
conn.open
if conn,state=0 then
msgbox "fail"
end if

解决方案 »

  1.   

    Dim strsql As String
    On Error GoTo handle
        strsql = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
        "" & App.Path & "\db1.mdb;Persist Security Info=False"
        con.Open strsql '连接数据库
        Exit Sub
    handle:
        MsgBox "数据库连接错误"
      

  2.   

    private sub  form_load()
       dim cnstr as string
       dim cn as new adodb.connection
       on error goto Err
       cnstr="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
        "" & App.Path & "\数据库名;Persist Security Info=False"
       cn.open cnstr'进行数据库的连接
       exit sub
    Err:
       Msgbox "数据库连接错误!!!错误为:" & Err.Description, 16, "错误"
    end sub
      

  3.   

    private sub  form_load()
       dim cnstr as string
       dim cn as new adodb.connection
       on error goto Err
       cnstr="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
        "" & App.Path & "\数据库名;Persist Security Info=False"
       cn.open cnstr'进行数据库的连接
       exit sub
    Err:IF ERR.Number=123 THEN
      MSGBOX “对应123的错误名称”
    ELSEIF ERR.Number=“456” THEN
      MSGBOX“对应456的错误名称”
    ENDIF
    END SUB
      

  4.   

    用错误处理
    不过最好能报出错误名称
    比如
    if err.number<>0 then
        msgbox err.description
        exit sub
    endif
      

  5.   

    记得加 On Error Resume Next