Results 1 to 15 of 15

Thread: ADO Connection Error handling

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2002
    Location
    Dover, NH
    Posts
    5

    ADO Connection Error handling

    I'm attempting to connect to various SQL DB's on different Servers
    by using the IP of each server.
    The problem is that if the IP has changed and I attempt to connect I get the old -2147467259 error when the app is attempting to Open the connection.

    Is there a way to trap this error so that if the Open doesn't occur
    I can handle it?

    Also can I incorporate the "ping.exe" into my code to update the
    DB ServerIP field???

    Dim cSQLConn As Connection
    Set cSQLConn = New Connection
    cSQLConn = "Provider=SQLOLEDB" & _
    ";Data Source=" & rsServer.Fields("ServerIP").Value & _
    ";User ID=" & rsServer.Fields("ServerDBLogin").Value & _
    ";Password=" & rsServer.Fields("ServerDBPassword").Value & _
    ";Initial Catalog=" & rsServer.Fields("DatabaseName").Value
    cSQLConn.CommandTimeout = 400
    cSQLConn.Open

    Thanks

    Bill

  2. #2
    Frenzied Member Mega_Man's Avatar
    Join Date
    Mar 2001
    Location
    North of England, South-East of Iceland
    Posts
    1,067
    VB Code:
    1. On Error goto Errorhandler
    2.  
    3. Dim cSQLConn As Connection
    4. Set cSQLConn = New Connection
    5. cSQLConn = "Provider=SQLOLEDB" & _
    6. ";Data Source=" & rsServer.Fields("ServerIP").Value & _
    7. ";User ID=" & rsServer.Fields("ServerDBLogin").Value & _
    8. ";Password=" & rsServer.Fields("ServerDBPassword").Value & _
    9. ";Initial Catalog=" & rsServer.Fields("DatabaseName").Value
    10. cSQLConn.CommandTimeout = 400
    11. cSQLConn.Open
    12.  
    13. ErrorHandler:
    14. Select Case err.Number
    15.     case -2147467259
    16.     msgbox "Error opening connection"
    17.     case Else
    18.     msgbox "Different error"
    19. End Select
    As for pinging a I.P address, you need loads of APIs. Do a search in the general VB questions forum. There are loads of eamples.

    Mega.
    "If at first you don't succeed, then skydiving is not for you"

  3. #3

    Thread Starter
    New Member
    Join Date
    Oct 2002
    Location
    Dover, NH
    Posts
    5
    Mega_Man

    Thanks for the reply...
    After attempting several error handling options this one seem to work best...

    Dim cSQLConn As Connection
    Set cSQLConn = New Connection
    cSQLConn = "Provider=SQLOLEDB" & _
    ";Data Source=" & rsServer.Fields("ServerIP").Value & _
    ";User ID=" & rsServer.Fields("ServerDBLogin").Value & _
    ";Password=" & rsServer.Fields("ServerDBPassword").Value & _
    ";Initial Catalog=" & rsServer.Fields("DatabaseName").Value
    cSQLConn.CommandTimeout = 400
    On Error Resume Next
    cSQLConn.Open
    If cSQLConn.State = 0 Then
    Goto ErrorHandler
    End If

    ErrorHandler:
    Select Case err.Number
    case -2147467259
    msgbox "Error opening connection"
    case Else
    msgbox "Different error"
    End Select

    I tried the option you offered and it works the first time but since
    this is part of a loop statement where I make several connections
    to different servers, the error handling fails when on the 2nd through N attempts.

    Thanks for the help

    Bill

  4. #4
    Frenzied Member Mega_Man's Avatar
    Join Date
    Mar 2001
    Location
    North of England, South-East of Iceland
    Posts
    1,067
    You might want to try placing a doevents just after you Connection.Open statement. This should ensure that the error trapping works every time. The other thing you can do in your loop is to only establish a connection after the last one has connected.
    VB Code:
    1. If Connection.State <> adStateConnected
    2. ' Dont establish another connection.
    Mega.
    Last edited by Mega_Man; Nov 4th, 2002 at 09:07 AM.
    "If at first you don't succeed, then skydiving is not for you"

  5. #5
    PowerPoster JPnyc's Avatar
    Join Date
    Oct 2002
    Location
    Manhattan
    Posts
    3,015
    As long as we're on the subject, I placed an errorhandler in a form, and I want to send there on error from other forms,subs. Do I call it like any procedure?

  6. #6
    Frenzied Member John McKernan's Avatar
    Join Date
    Jan 2002
    Location
    SE PA
    Posts
    1,295
    Originally posted by DUNSEL
    As long as we're on the subject, I placed an errorhandler in a form, and I want to send there on error from other forms,subs. Do I call it like any procedure?
    Not real sure what you're asking. Can you give some more info or post a sample of your code?

  7. #7
    PowerPoster JPnyc's Avatar
    Join Date
    Oct 2002
    Location
    Manhattan
    Posts
    3,015
    Basically asking the syntax. Near the top of the sub, the words, On Error, then Goto, then the label of the error handler starting with formname.errorhandlername?

  8. #8
    Frenzied Member Mega_Man's Avatar
    Join Date
    Mar 2001
    Location
    North of England, South-East of Iceland
    Posts
    1,067
    If you want to call an error handler from anywhere in you project (irrespective of form), then place all you error handling code in a public module.
    You can then call it like such.
    VB Code:
    1. On Error Goto ErrorHandler
    2. 'code
    3. 'code
    4. ErrorHandler:
    5. Call ErrorRoutine ("module name")
    6.  
    7. Public Sub ErrorRoutine (Dim Error As String)
    8. msgbox "A Error has occured in module " & Error
    9. End Sub
    Mega.
    "If at first you don't succeed, then skydiving is not for you"

  9. #9
    Frenzied Member John McKernan's Avatar
    Join Date
    Jan 2002
    Location
    SE PA
    Posts
    1,295
    OK, I'm sorry but I might still be a little confused here, but a normal error handling routine would look like this:
    VB Code:
    1. Public Sub SomeSub()
    2.    On Error Goto ErrHandler
    3.  
    4.     'sub code goes here
    5.  
    6.     Exit Sub  ' usually so you don`t execute the error handler unless there is an error
    7. ErrHandler:
    8.     'Error handling code here
    9. End Sub
    Now, if you to call this ErrHandler from another form or sub, you cannot.

    You can, however, write a general error handler as a stand-alone sub or function and then call that from other forms, modules.

    Did this help? Like I said, I think I'm still a little confused about what you are trying to do.

  10. #10
    PowerPoster JPnyc's Avatar
    Join Date
    Oct 2002
    Location
    Manhattan
    Posts
    3,015
    Thank you. I knew it needed to be called near the beginning of subs where errors might be likely, and then labeled, but was unsure of the syntax.

  11. #11
    Frenzied Member John McKernan's Avatar
    Join Date
    Jan 2002
    Location
    SE PA
    Posts
    1,295
    Originally posted by DUNSEL
    Thank you. I knew it needed to be called near the beginning of subs where errors might be likely, and then labeled, but was unsure of the syntax.
    Glad it helped.

  12. #12
    Frenzied Member John McKernan's Avatar
    Join Date
    Jan 2002
    Location
    SE PA
    Posts
    1,295
    Originally posted by DUNSEL
    Thank you. I knew it needed to be called near the beginning of subs where errors might be likely, and then labeled, but was unsure of the syntax.
    Yes, it does have to be labelled (unles you use line numbers), but the On Error Goto does not have to be the first line in a sub/function. It just has to appear before yuo anticipate the error.

  13. #13
    PowerPoster JPnyc's Avatar
    Join Date
    Oct 2002
    Location
    Manhattan
    Posts
    3,015
    I wanted to set up a central public error handler, and wasn't sure if I needed to call the form name or not, like you do in any sub call from another form.

  14. #14
    PowerPoster JPnyc's Avatar
    Join Date
    Oct 2002
    Location
    Manhattan
    Posts
    3,015
    You've all been most helpful. Can any of you direct me to the best book/site for learning VB procedures/methods/operators on my own? I have basic programming knowledge, but very sketchy on VB. Thanks.

  15. #15
    Frenzied Member John McKernan's Avatar
    Join Date
    Jan 2002
    Location
    SE PA
    Posts
    1,295
    A public central error handler should be placed in a standard .bas module.

    Public subs/functions in modules do not require the Module name. For example, assuming a module named modExample and a sub name MySub, either calling syntax is fine:
    VB Code:
    1. MySub
    2. modExample.MySub

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