Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,588
Default stop macro if no SQL connection found

Try

on error resume next
cnn1.Open
on error goto 0

if cnn1.state<adStateOpen then
debug.print "Connection failed!"
exit sub
end if

Tim

"Dan" wrote in message
oups.com...
Hello -

I have the following macro and would like to end it if no SQL
connection is found. The code process for about a minute before it
will get a run-time error when no connection is present. I would like
the macro to end with no warning boxes; basically invisible to the end
user.

Private Sub Workbook_Open()
Dim cnn1 As ADODB.Connection
Dim runspcmd As ADODB.Command
Dim x As Integer

Set cnn1 = New ADODB.Connection
cnn1.ConnectionString = "Provider=sqloledb;Data
Source=000.00.000.000;Initial Catalog=brdatadb;User
Id=xxx;Password=xxx;"
cnn1.ConnectionTimeout = 60
cnn1.Open

Set runspcmd = New ADODB.Command
runspcmd.ActiveConnection = cnn1
runspcmd.CommandTimeout = 60

x = 0

Set rs = New ADODB.Recordset
rs.CursorType = adOpenDynamic
rs.CursorLocation = adUseClient

runspcmd.CommandText = "select vendor_name, vendor_code from " & _
" vendor order by
vendor_name "
rs.Open runspcmd

If Not rs.EOF Then
Me.Worksheets.Item(2).Range("A1").CopyFromRecordse t rs
End If

rs.Close
cnn1.Close
End Sub

Thanks for any assistance!
Dan



  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,391
Default stop macro if no SQL connection found

As the OP is using the default value (True) of the Options argument to
..Open, the connection is opened synchronously, so this works.

However, the .State is actually a bit mask so you should check the .State
with

If (cnn1.State And adStateOpen) = adStateOpen Then
'Connection is open
Else
'Connection is closed
End If

Read the help on the State Property, although the examples do not make this
clear.
This was recently pointed out in:
http://groups.google.co.uk/group/mic...73fcb85898b202

NickHK

"Tim Williams" <timjwilliams at gmail dot com wrote in message
...
Try

on error resume next
cnn1.Open
on error goto 0

if cnn1.state<adStateOpen then
debug.print "Connection failed!"
exit sub
end if

Tim

"Dan" wrote in message
oups.com...
Hello -

I have the following macro and would like to end it if no SQL
connection is found. The code process for about a minute before it
will get a run-time error when no connection is present. I would like
the macro to end with no warning boxes; basically invisible to the end
user.

Private Sub Workbook_Open()
Dim cnn1 As ADODB.Connection
Dim runspcmd As ADODB.Command
Dim x As Integer

Set cnn1 = New ADODB.Connection
cnn1.ConnectionString = "Provider=sqloledb;Data
Source=000.00.000.000;Initial Catalog=brdatadb;User
Id=xxx;Password=xxx;"
cnn1.ConnectionTimeout = 60
cnn1.Open

Set runspcmd = New ADODB.Command
runspcmd.ActiveConnection = cnn1
runspcmd.CommandTimeout = 60

x = 0

Set rs = New ADODB.Recordset
rs.CursorType = adOpenDynamic
rs.CursorLocation = adUseClient

runspcmd.CommandText = "select vendor_name, vendor_code from " & _
" vendor order by
vendor_name "
rs.Open runspcmd

If Not rs.EOF Then
Me.Worksheets.Item(2).Range("A1").CopyFromRecordse t rs
End If

rs.Close
cnn1.Close
End Sub

Thanks for any assistance!
Dan





  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,588
Default stop macro if no SQL connection found

Good to know - thanks.

Tim


"NickHK" wrote in message
...
As the OP is using the default value (True) of the Options argument to
.Open, the connection is opened synchronously, so this works.

However, the .State is actually a bit mask so you should check the .State
with

If (cnn1.State And adStateOpen) = adStateOpen Then
'Connection is open
Else
'Connection is closed
End If

Read the help on the State Property, although the examples do not make
this
clear.
This was recently pointed out in:
http://groups.google.co.uk/group/mic...73fcb85898b202

NickHK



  #4   Report Post  
Posted to microsoft.public.excel.programming
dan dan is offline
external usenet poster
 
Posts: 41
Default stop macro if no SQL connection found

On Feb 28, 9:57 pm, "Tim Williams" <timjwilliams at gmail dot com
wrote:
Try

on error resume next
cnn1.Open
on error goto 0

if cnn1.state<adStateOpen then
debug.print "Connection failed!"
exit sub
end if

Tim

"Dan" wrote in message

oups.com...



Hello -


I have the following macro and would like to end it if no SQL
connection is found. The code process for about a minute before it
will get a run-time error when no connection is present. I would like
the macro to end with no warning boxes; basically invisible to the end
user.


Private Sub Workbook_Open()
Dim cnn1 As ADODB.Connection
Dim runspcmd As ADODB.Command
Dim x As Integer


Set cnn1 = New ADODB.Connection
cnn1.ConnectionString = "Provider=sqloledb;Data
Source=000.00.000.000;Initial Catalog=brdatadb;User
Id=xxx;Password=xxx;"
cnn1.ConnectionTimeout = 60
cnn1.Open


Set runspcmd = New ADODB.Command
runspcmd.ActiveConnection = cnn1
runspcmd.CommandTimeout = 60


x = 0


Set rs = New ADODB.Recordset
rs.CursorType = adOpenDynamic
rs.CursorLocation = adUseClient


runspcmd.CommandText = "select vendor_name, vendor_code from " & _
" vendor order by
vendor_name "
rs.Open runspcmd


If Not rs.EOF Then
Me.Worksheets.Item(2).Range("A1").CopyFromRecordse t rs
End If


rs.Close
cnn1.Close
End Sub


Thanks for any assistance!
Dan- Hide quoted text -


- Show quoted text -


Thanks Tim, works great!

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
return cells below stop when word found bwilk77 Excel Discussion (Misc queries) 1 April 2nd 09 05:50 PM
Stop a loop when certain data is found jsmith Excel Discussion (Misc queries) 3 June 2nd 08 10:12 PM
always recheck data connection library for latest connection strin FurmanGG Excel Discussion (Misc queries) 0 September 14th 07 04:48 PM
Stop a Macro if no Data found tedd13 Excel Programming 1 November 8th 06 04:05 PM
Stop the loop when found. hfazal Excel Programming 1 February 16th 06 11:57 PM


All times are GMT +1. The time now is 12:33 PM.

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

About Us

"It's about Microsoft Excel"