Results 1 to 6 of 6

Thread: listing the system DSN in a combobox

  1. #1

    Thread Starter
    Lively Member
    Join Date
    May 2008
    Posts
    68

    listing the system DSN in a combobox

    need code to list the system DSN in my system in a combo box

  2. #2
    Lively Member
    Join Date
    Oct 2007
    Posts
    68

    Re: listing the system DSN in a combobox

    One way to do it is to look in the odbc data sources from the ODBC.INI file.

    Code:
    Private Declare Function GetWindowsDirectory Lib "kernel32" Alias "GetWindowsDirectoryA" _
                    (ByVal lpBuffer As String, _
                     ByVal nSize As Long) As Long
    
    Public Declare Function GetPrivateProfileSection Lib "kernel32" Alias "GetPrivateProfileSectionA" _
                    (ByVal lpAppName As String, _
                     ByVal lpReturnedString As String, _
                     ByVal nSize As Long, _
                     ByVal lpFileName As String) As Long
    
    
    Private Sub LoadDriverCombo()
    
        Dim RetVal      As Long
        Dim I           As Long
        Dim ODBCDir     As String
        Dim strReturn   As String
        Dim ODBCSrc()   As String
        
        strReturn = Space$(4096 & " ")
    
        '//need to look in system directory instead of registry since some
        '//data sources don't update registry entries 
    
        '//get the default system directory
        ODBCDir = GetWinDir
        
        '//odbc list should be in path
        ODBCDir = FixPathName(ODBCDir) & "odbc.ini"
        
        '//retrieve the odbc information
        RetVal = GetPrivateProfileSection("ODBC 32 bit Data Sources", strReturn, Len(strReturn) - 1, ODBCDir)
        
        strReturn = UCase$(strReturn)
    
        '//found the ODBC file - now parse it into the combo
        If RetVal > 32 Then
            ODBCSrc() = Split(UCase$(strReturn), Chr(0))
                
            For I = LBound(ODBCSrc) To UBound(ODBCSrc)
                If Trim$(ODBCSrc(I)) <> "" Then
                    cboDataSource.AddItem (Left$(ODBCSrc(I), InStr(1, ODBCSrc(I), "=") - 1))
                End If
            Next
    
        Else
           error handle
        End If
    
    End Sub
    
    
    Function GetWinDir() As String
    
        Dim RetVal      As Long
        Dim strTemp     As String
        
        '//create string for return value
        strTemp = Space$(256)
        RetVal = Len(strTemp)
        
        '//call the API
        Call GetWindowsDirectory(strTemp, RetVal)
        
        '//trim off the trailing null added by the API
        If InStr(Trim(strTemp), Chr$(0)) Then
            strTemp = Left$(strTemp, InStr(strTemp, Chr$(0)) - 1)
        End If
        
        GetWinDir = strTemp
        
    End Function

  3. #3
    New Member
    Join Date
    Jul 2008
    Posts
    1

    Re: listing the system DSN in a combobox

    Hi,

    I need to display the list of system DSN in a combobox(VB 6.0).
    I tried with your code in this thread. But it is not listing the DSN's.
    Do I need to Change anything in the properties of combobox?
    I am not well versed with VB. So help me.

  4. #4

    Thread Starter
    Lively Member
    Join Date
    May 2008
    Posts
    68

    Re: listing the system DSN in a combobox

    what is 'FixPathName' in the code...its left undeclared..it throws up a compile time error

  5. #5
    Lively Member
    Join Date
    Oct 2007
    Posts
    68

    Re: listing the system DSN in a combobox

    Sry, that's just a home-grown string routine that appends the "\" to the end of a file path if it doesn't already contain one. It's not required.

    Code:
    Public Function FixPathName(PathName As String) As String
    
        FixPathName = PathName & IIf(Right(PathName, 1) = "\", "", "\")
        
    End Function

  6. #6
    Lively Member
    Join Date
    Oct 2007
    Posts
    68

    Re: listing the system DSN in a combobox

    Usually your dsn is listed in the windows directory, ODBC.INI file.

    So all this code does read this ini file and parse the associated driver information from it and then put the result into a combo box.

    Another method is to go into the registry under HKEY_LOCAL_MACHINE\SOFTWARE\ODBC\ODBC.INI and return the system dsns that way.

    If you want to return user dsns, you'll need to read HKEY_CURRENT_USER instead of hkey_local_machine.

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