Results 1 to 6 of 6

Verify Connection

Threaded View

  1. #1
    Forum Contributor
    Join Date
    06-03-2008
    Posts
    387

    Verify Connection

    Hi,

    I have some VBA code that connects to a SQL Database that's on an internal network. This code does other things where they don't need access to the Database. So, when an individual is at home, I'd like to be able to detect in the quickest way possible whether they can reach the Database or not.

    Right now, my code catches the error when it tries to open the connection. When it can't find the DB, it returns the error:
    [DBNETLIB][ConnectionOpen (Connect()).]SQL Server does not exist or access is denied

    If they're on the network or have a VPN connection, it normally takes under a second to connect, but this error takes 11 seconds, so I'd like to find a way to make it error out sooner.
    Public Sub ExampleDBCall()
        On Error GoTo errHandler
        Dim cnPubs As Object
        Dim cnCmd As Object
        Set cnPubs = CreateObject("ADODB.Connection")
        Set cnCmd = CreateObject("ADODB.Command")
        cnPubs.Open strConn '<---Connection String to Database
        cnCmd.ActiveConnection = cnPubs
            
        With cnCmd
                .CommandText = "storeuser_rw.StoredProcedureName"
                .CommandType = 4 'adCmdStoredProc
                .Parameters.Refresh
                .Parameters("@Field") = value
                .Execute
        End With
        cnPubs.Close
        Set cnPubs = Nothing
        Set cnCmd = Nothing
        Exit Sub
    errHandler:
        If cnPubs Is Nothing Then
        Else
            If cnPubs.State = 1 Then
                cnPubs.Close
            End If
            Set cnPubs = Nothing
        End If
        Set cnCmd = Nothing
        MsgBox "An error occured." & vbCr & Err.Description
    End Sub
    Last edited by yawnzzzz; 08-07-2011 at 03:13 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1