I'd like to read a list of the ODBC DSNs set up on the computer. I thought I
have seen how to do this but I can't seem to dag it. Can anyone help?
Thanks,
Bernie
Voilà...
'API to obtain list of ODBC connections
Private Declare Function SQLAllocEnv Lib "ODBC32.DLL" (ByRef env As
Integer) As Short
Private Declare Function SQLFreeHandle Lib "odbc32.dll" (ByVal
handleType As Short, ByVal Handle As Integer) As Short
Private Declare Function SQLDataSources Lib "ODBC32.DLL" (ByVal
WindowHandle As Integer, ByVal Direction As Short, ByVal DSNBuffer As
String, ByVal DSNBufferLength As Short, ByRef DSNLength As Short,
ByVal DESCBuffer As String, ByVal DESCBufferLength As Short, ByRef
DESCLength As Short) As Short
Public Shared Sub LoadDSNList(ByVal ComboBoxCtrl As ComboBox)
Const SQL_BUFFER_SIZE As Short = 1024
Const SQL_SUCCESS As Short = 0
Const SQL_SUCCESS_WITH_INFO As Short = 1
Const SQL_FETCH_FIRST As Short = 1
Const SQL_HANDLE_ENV As Integer = 1
Dim env, RetInt As Integer
Dim RetShort As Short = SQL_SUCCESS
Dim BufferDSN, BufferDesc As String
Dim LengthDSN, LengthDesc As Short
Dim DSNName As String
ComboBoxCtrl.Items.Clear()
RetInt = SQLAllocEnv(env)
If (RetInt = SQL_SUCCESS) Or (RetInt = SQL_SUCCESS_WITH_INFO) Then
'Get the DSN names & descriptions
While (RetShort = SQL_SUCCESS) Or (RetShort =
SQL_SUCCESS_WITH_INFO)
BufferDSN = Space(SQL_BUFFER_SIZE)
BufferDesc = Space(SQL_BUFFER_SIZE)
RetShort = SQLDataSources(env, SQL_FETCH_FIRST, BufferDSN,
SQL_BUFFER_SIZE, _
LengthDSN, BufferDesc,
SQL_BUFFER_SIZE, LengthDesc)
DSNName = BufferDSN.Substring(0, LengthDSN)
If (DSNName <> Space(LengthDSN)) Then
ComboBoxCtrl.Items.Add(DSNName)
End While
SQLFreeHandle(SQL_HANDLE_ENV, env)
env = Nothing
End If
If (ComboBoxCtrl.Items.Count < 1) Then
ComboBoxCtrl.Items.Add(My.Resources.S_MISC_NO_ODBC_CONNECTIONS_AVAILABLE)
End Sub