|
-
Jul 1st, 2008, 10:52 AM
#1
Thread Starter
Lively Member
listing the system DSN in a combobox
need code to list the system DSN in my system in a combo box
-
Jul 1st, 2008, 03:01 PM
#2
Lively Member
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
-
Jul 1st, 2008, 11:28 PM
#3
New Member
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.
-
Jul 2nd, 2008, 06:35 AM
#4
Thread Starter
Lively Member
Re: listing the system DSN in a combobox
what is 'FixPathName' in the code...its left undeclared..it throws up a compile time error
-
Jul 3rd, 2008, 12:29 PM
#5
Lively Member
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
-
Jul 6th, 2008, 01:58 PM
#6
Lively Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|