Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
tod tod is offline
external usenet poster
 
Posts: 114
Default Error Handling when database not available

Here's my newbie questions o' the day:

I'm using ADO to connect to a SQL database. Here's my
connection string:

cn.Open "Driver={SQL
Server};Server=MyIPAddress;Database=DatabaseName;U ID=MyID;
PWD=MyPassword"

There may be times when the database is not available.
Currently it just gives an automation error. Is there a
way I can use code to first test for the availability of
the database?

tod
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,327
Default Error Handling when database not available

Hi

Use a small function for this. Rewrite the SQL in it to do a search for a
single indexed number or a primary key from a small table so it executes
fast:

Function DatabaseOK() As Boolean
Dim cn As New ADODB.Connection
Dim rst As New ADODB.Recordset
On Error GoTo TheEnd
cn.Open "Driver={SQLServer};Server=MyIPAddress;" & _
"Database=DatabaseName;UID=MyID;PWD=MyPassword "
rst.Open _
"SELECT ID FROM Customers WHERE ID=1", _
cn, adOpenForwardOnly, adLockReadOnly
DoEvents
rst.Close
DatabaseOK = True
Exit Function
TheEnd:
DatabaseOK = False
On Error Resume Next
rst.Close
cn.Close
End Function

And in your main code:

If DatabaseOK = False then
Msgbox "System is down. Miller time!"
Exit sub
end if

HTH. Best wishes Harald

"Tod" skrev i melding
...
Here's my newbie questions o' the day:

I'm using ADO to connect to a SQL database. Here's my
connection string:

cn.Open "Driver={SQL
Server};Server=MyIPAddress;Database=DatabaseName;U ID=MyID;
PWD=MyPassword"

There may be times when the database is not available.
Currently it just gives an automation error. Is there a
way I can use code to first test for the availability of
the database?

tod



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Handling #NUM! error Michel Khennafi Excel Worksheet Functions 1 February 26th 07 08:49 PM
MS-Excel and Complex Database Handling? Mike Excel Discussion (Misc queries) 2 May 18th 06 08:14 PM
Error Handling bw Excel Programming 3 June 20th 04 06:43 PM
Error handling V. Roe Excel Programming 2 February 27th 04 08:04 PM
Error Handling James Agostinho Excel Programming 1 January 30th 04 06:40 AM


All times are GMT +1. The time now is 02:18 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"