Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

adodb connection error handling

Status
Not open for further replies.

galorin

MIS
Nov 22, 2007
154
GB
I have the following code in place to check for a connection to a database. It is supposed to do stuff on error instead of showing a cryptic error box. (run time error blah blah blah... can't connect to MySQL server on server .....
Code:
Private Sub Form_Load()

Set cn = New ADODB.Connection
On Error GoTo connErr
With cn
 If .State = 0 Then
  .ConnectionString = DB_CONNECT
  .Open
  islocal = False
 End If
End With
 
sub_exit:
Exit Sub

connErr:
MsgBox "Sorry, couldn't connect to database.  Please try again later", vbOKOnly
Me.Contacts.Enabled = False
Me.OpenQuotes.Enabled = False
Me.OpDirectory.Enabled = False
Me.RepBtn.Enabled = False
Resume sub_exit
End Sub
Right now, I get it to error by just unplugging my network cable, as I am connecting to a remote server. Instead of getting my message box and errors, it is spitting out the cryptic (to joe bloggs) error briefly described above when trying to run .open. Am I missing anything there?
 
In a VB code window, select Tools / Options / General and make sure that "Break on Unhandled Errors" is selected. If "Break on All Errors" is selected then your error handler will be ignored.
 
Furthermore, I'd use the cn.Errors collection.

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top