|
-
Nov 3rd, 2002, 10:07 AM
#1
Thread Starter
New Member
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
-
Nov 3rd, 2002, 07:46 PM
#2
Frenzied Member
VB Code:
On Error goto Errorhandler
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
ErrorHandler:
Select Case err.Number
case -2147467259
msgbox "Error opening connection"
case Else
msgbox "Different error"
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"
-
Nov 4th, 2002, 08:03 AM
#3
Thread Starter
New Member
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
-
Nov 4th, 2002, 09:03 AM
#4
Frenzied Member
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:
If Connection.State <> adStateConnected
' 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"
-
Nov 5th, 2002, 05:26 PM
#5
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?
-
Nov 5th, 2002, 05:29 PM
#6
Frenzied Member
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?
-
Nov 8th, 2002, 05:44 PM
#7
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?
-
Nov 8th, 2002, 08:01 PM
#8
Frenzied Member
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:
On Error Goto ErrorHandler
'code
'code
ErrorHandler:
Call ErrorRoutine ("module name")
Public Sub ErrorRoutine (Dim Error As String)
msgbox "A Error has occured in module " & Error
End Sub
Mega.
"If at first you don't succeed, then skydiving is not for you"
-
Nov 8th, 2002, 08:04 PM
#9
Frenzied Member
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:
Public Sub SomeSub()
On Error Goto ErrHandler
'sub code goes here
Exit Sub ' usually so you don`t execute the error handler unless there is an error
ErrHandler:
'Error handling code here
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.
-
Nov 8th, 2002, 08:04 PM
#10
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.
-
Nov 8th, 2002, 08:06 PM
#11
Frenzied Member
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.
-
Nov 8th, 2002, 08:07 PM
#12
Frenzied Member
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.
-
Nov 8th, 2002, 08:07 PM
#13
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.
-
Nov 8th, 2002, 08:10 PM
#14
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.
-
Nov 8th, 2002, 08:10 PM
#15
Frenzied Member
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:
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|