Results 1 to 2 of 2

Thread: How to get a list of the available DSN name?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 1999
    Location
    Taipei
    Posts
    318

    Cool

    My problem try to display a list of the available DSN for
    user to choose from? Other than directly read the ODBC.ini,
    is there any API/or so, that I can use to get the list?

    Many Thanks

  2. #2
    old fart Frans C's Avatar
    Join Date
    Oct 1999
    Location
    the Netherlands
    Posts
    2,926
    From the Visdata sample included with vb:

    Code:
    Private Declare Function SQLDataSources Lib "ODBC32.DLL" (ByVal henv&, ByVal fDirection%, ByVal szDSN$, ByVal cbDSNMax%, pcbDSN%, ByVal szDescription$, ByVal cbDescriptionMax%, pcbDescription%) As Integer
    Private Declare Function SQLAllocEnv% Lib "ODBC32.DLL" (env&)
    Const SQL_SUCCESS As Long = 0
    Const SQL_FETCH_NEXT As Long = 1
    
    Sub GetDSNsAndDrivers()
      On Error Resume Next
      
      Dim i As Integer
      Dim sDSNItem As String * 1024
      Dim sDRVItem As String * 1024
      Dim sDSN As String
      Dim sDRV As String
      Dim iDSNLen As Integer
      Dim iDRVLen As Integer
      Dim lHenv As Long     'handle to the environment
    
      cboDSNList.AddItem "(None)"
    
      'get the DSNs
      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 = VBA.Left(sDSNItem, iDSNLen)
          sDRV = VBA.Left(sDRVItem, iDRVLen)
            
          If sDSN <> Space(iDSNLen) Then
            cboDSNList.AddItem sDSN
            cboDrivers.AddItem sDRV
          End If
        Loop
      End If
      'remove the dupes
      If cboDSNList.ListCount > 0 Then
        With cboDrivers
          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
      cboDSNList.ListIndex = 0
      
    End Sub

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