Results 1 to 6 of 6

Thread: Testing a connection to see if it works

  1. #1
    Dimension
    Guest

    Testing a connection to see if it works

    I have a connection string i want to test to see if its valid...meaning it can connect to a db. How is this done?

  2. #2
    Hyperactive Member MetallicaD's Avatar
    Join Date
    Feb 2001
    Location
    Tallahassee, FL
    Posts
    488
    here is what I do to make and test connection strings. create a new textfile.. rename it to <anything>.UDL Then open up the file and set your connection properties (choose providor, point to your db) then there is a button called labeled 'Test Connection'. This will, obviously, test your connection.

    Now, here is the cool part.. open up notepad.. drag this udl file and drop it into notepad.. this will write out your connection string

    -mcd
    [vbcode]
    '*****************************
    MsgBox "MCD :: [email protected]", vbInformation + vbOKOnly, "User"
    '*****************************
    [/vbcode]

  3. #3
    Dimension
    Guest
    i've already done all of that...i just want a way to see if the connectionstring supplied is valid or not

  4. #4
    Dimension
    Guest
    ^ without having to hit the test connection....in code

  5. #5
    Hyperactive Member Steve Stunning's Avatar
    Join Date
    Jul 1999
    Location
    Fairfax, Virginia
    Posts
    314
    I also had the same questions and this is what I deemed to do the trick.



    Function VerifyConnection(ConnectionString As String) As Boolean

    On Error GoTo ErrorHandler

    Dim Cn As New ADODB.Connection

    Cn.CursorLocation = adUseClient
    Cn.ConnectionString = ConnectionString
    Cn.ConnectionTimeout = 3


    ' open connection
    Cn.Open

    ' Close connection
    Cn.Close


    ErrorHandler:

    If Err.Number <> 0 Then
    VerifyConnection = False
    else
    VerifyConnection = True
    end if

    End Function
    Steve Stunning

  6. #6
    Hyperactive Member fasi's Avatar
    Join Date
    Nov 2000
    Posts
    474
    You can also use the following code to test the connection say cnn is your ado connection

    you can use

    VB Code:
    1. if cnn.state <> adStateOpen then
    2.     msgbox"Cannot open the connection"
    3. endif

Posting Permissions

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



Click Here to Expand Forum to Full Width