CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Nov 2000
    Posts
    7

    Trapping ADO connection error

    It seems strange to me that the Connection object doesn't expect failed connections and that this has to be trapped with On Error. Am I missing something?


  2. #2
    Join Date
    Aug 2000
    Location
    Ottawa, Canada
    Posts
    469

    Re: Trapping ADO connection error

    What type of DB engine you're connectin to?


  3. #3
    Join Date
    Nov 2000
    Posts
    7

    Re: Trapping ADO connection error

    SQL Server


  4. #4
    Join Date
    Aug 2000
    Location
    Ottawa, Canada
    Posts
    469

    Re: Trapping ADO connection error

    In this case I had the same problem. So, to catch errors during connection I use this function before connecting with ADO.

    '------------------------------------------------------------------------------------
    private Function lConnectSQLDB(strServer_p as string, _
    strLogin_p as string, _
    strPassw_p as string) as Long
    '
    ' tries to connect to SQL DB, returns meaningful error if not successfully
    '------------------------------------------------------------------------------------
    on error GoTo ErrorHandler
    '
    ' to connect to SQL Server/MSDE
    Dim oSQLServer as Object
    set oSQLServer = CreateObject("SQLDMO.SQLServer")
    '
    ' set timeout
    oSQLServer.LoginTimeout = 60
    '
    ' try to connect
    oSQLServer.Connect strServer_p, strSQLLogin_g, strPassw_p
    '
    ' close connection if it was successful
    oSQLServer.Close
    lConnectSQLDB = 0
    '
    Exit Function
    '
    ErrorHandler:
    oSQLServer.Close
    lConnectSQLDB = Err.Number
    Select Case Err.Number
    Case 18456
    ' we're here if SQL Server refused provided login and password
    Case 440
    ' we're here if SQL Server doesn't respond (doesn't exist)
    Case 429
    ' we're here if user doesn't have SQL client
    Case else
    MsgBox Err.Number, Err.Description, vbCritical
    End Select
    End Function



    To use this SQLDMO stuff your PC should have installed SQL Server or MSDE or SQL client.
    Or none of these, but several dll's in special place.

    I'm glad, if it will help you.


  5. #5
    Join Date
    Nov 2000
    Posts
    7

    Re: Trapping ADO connection error

    Thank you for the code. I created a similar function using the ADO connection object. But that doesn't provide me with any specifics on the nature of the error. I'll check out SQLDMO.



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured