System DSNs

Posted on 2000-01-14
Medium Priority
Last Modified: 2012-05-04
How can I get a list of the current System DSNs from visual basic?

Thanks in advance.
Question by:rj_b
  • 2

Accepted Solution

Gogman earned 240 total points
ID: 2354536
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


    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)


                        i = i + 1

                    End If


            End If

        End With

    End If

    List1.ListIndex = 0

End Sub

---- End Code ----



Author Comment

ID: 2354684
Is there a way to make it so it just gives me system DSNs rather than both system and file DSNs?

Author Comment

ID: 2354698
Is there a way to make it so it just gives me system DSNs rather than both system and file DSNs?

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Suggested Courses
Course of the Month7 days, 2 hours left to enroll

593 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question