System DSNs

How can I get a list of the current System DSNs from visual basic?

Thanks in advance.
rj_bAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

GogmanCommented:
Create a Form with a ListBox on it.

Paste this code into the form and run.

---- Start Code ----

Option Explicit

Const SQL_SUCCESS As Long = 0
Const SQL_FETCH_NEXT As Long = 1

Private Declare Function SQLAllocEnv% Lib "ODBC32.DLL" (env&)
Private Declare Function SQLDataSources Lib "ODBC32.DLL" (ByVal henv&, ByVal fDirection%, ByVal szDSN$, ByVal cbDSNMax%, pcbDSN%, ByVal szDescription$, ByVal cbDescriptionMax%, pcbDescription%) As Integer
Private Sub Form_Load()

    On Local Error Resume Next

    Dim i As Integer
    Dim lHenv As Long
    Dim sDSN As String
    Dim sDRV As String
    Dim iDSNLen As Integer
    Dim iDRVLen As Integer
    Dim sDSNItem As String * 1024
    Dim sDRVItem As String * 1024

    ' list drivers
    If SQLAllocEnv(lHenv) <> -1 Then

        Do Until i <> SQL_SUCCESS

            sDSNItem = Space$(1024)
            sDRVItem = Space$(1024)

            i = SQLDataSources(lHenv, SQL_FETCH_NEXT, sDSNItem, 1024, iDSNLen, sDRVItem, 1024, iDRVLen)

            sDSN = Left$(sDSNItem, iDSNLen)
            sDRV = Left$(sDRVItem, iDRVLen)
               
            If sDSN <> Space(iDSNLen) Then

                List1.AddItem "DSN...: " & sDSN & " - Driver: " & sDRV

            End If

        Loop

    End If

    'remove duplicates
    If List1.ListCount > 0 Then

        With List1

            If .ListCount > 1 Then

                i = 0

                While i < .ListCount

                    If .List(i) = .List(i + 1) Then

                        .RemoveItem (i)

                    Else

                        i = i + 1

                    End If

                Wend

            End If

        End With

    End If

    List1.ListIndex = 0

End Sub

---- End Code ----

Enjoy!

gogman
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rj_bAuthor Commented:
Is there a way to make it so it just gives me system DSNs rather than both system and file DSNs?
0
rj_bAuthor Commented:
Is there a way to make it so it just gives me system DSNs rather than both system and file DSNs?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.