API call to get List of ODBC DSNs

Vlip

New Member
Joined
Mar 10, 2002
Messages
49
Hi,

Does anyone have an API that will allow me to get a list of ODBC data sources defined in the Windows ODBC Control Panel?

Thanks in advance,
Vlip
 
I'm sorry; we're still not on the same playing field. I will give this one more try and then I will just give up.
Here goes: If you open the ODBC control panel (on Windows 2000 it is in "Administrative Tools" then "Data Sources (ODBC)") you will get a dialog box titled: ODBC Data Source Administrator with 7 tabs along the top. The first two are "User DSN" and "System DSN". On either of these tabs, there is a two column listbox called: "User Data Sources" and "System Data Sources" respectively. Each list has two columns labeled "Name" and "Driver". I want that same list. I don't need the driver column, just the name column. If I can get the User DSN list then, I can get the other. The only difference is the path and that has been amply spelled out. So, just tell me how to get that list and we'll be home free! Again, I'm sorry I've taken up so much of your time. I appreciate your help.

Best regards,
Vlip
 
Upvote 0

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
Sorry a wild stab, can you not importthe same table twice ubder ODBC MS Query ruling this will work,, else i suggets asking the IT dept, i cant see the data from ghere somethings a drif..i agree..

sorry of little help... i wish i could do more, i really do..

thought...


winy2k do you use Kopen3Live???
 
Upvote 0
Hi
This will load a string array with user and sys DSNs.
As is, places on sheet1 A1:A?
Edit to suit.
If this is not what you need then I can not help you.
Good Day.
Tom

<table border="1" bgcolor="White"><caption ALIGN=left><font size="2" face=Courier New>Example VBA Code:</FONT></caption><tr><td><font size="2" face=Courier New>  <font color="#0000A0">Private</font> <font color="#0000A0">Declare</font> <font color="#0000A0">Function</font> SQLDataSources <font color="#0000A0">Lib</font> "odbc32.dll" _
     (ByVal hEnv <font color="#0000A0">As</font> Long, _
      <font color="#0000A0">ByVal</font> fDirection <font color="#0000A0">As</font> Integer, _
      <font color="#0000A0">ByVal</font> szDSN <font color="#0000A0">As</font> String, _
      <font color="#0000A0">ByVal</font> cbDSNMax <font color="#0000A0">As</font> Integer, _
      pcbDSN <font color="#0000A0">As</font> Integer, _
      <font color="#0000A0">ByVal</font> szDescription <font color="#0000A0">As</font> String, _
      <font color="#0000A0">ByVal</font> cbDescriptionMax <font color="#0000A0">As</font> Integer, _
      pcbDescription <font color="#0000A0">As</font> Integer) <font color="#0000A0">As</font> <font color="#0000A0">Long</font>
  
  <font color="#0000A0">Private</font> <font color="#0000A0">Declare</font> <font color="#0000A0">Function</font> SQLAllocHandle <font color="#0000A0">Lib</font> "odbc32.dll" _
     (ByVal HandleType <font color="#0000A0">As</font> Integer, _
      <font color="#0000A0">ByVal</font> InputHandle <font color="#0000A0">As</font> Long, _
      OutputHandlePtr <font color="#0000A0">As</font> Long) <font color="#0000A0">As</font> <font color="#0000A0">Long</font>
  
  <font color="#0000A0">Private</font> <font color="#0000A0">Declare</font> <font color="#0000A0">Function</font> SQLSetEnvAttr <font color="#0000A0">Lib</font> "odbc32.dll" _
     (ByVal EnvironmentHandle <font color="#0000A0">As</font> Long, _
      <font color="#0000A0">ByVal</font> dwAttribute <font color="#0000A0">As</font> Long, _
      <font color="#0000A0">ByVal</font> ValuePtr <font color="#0000A0">As</font> Long, _
      <font color="#0000A0">ByVal</font> StringLen <font color="#0000A0">As</font> Long) <font color="#0000A0">As</font> <font color="#0000A0">Long</font>
    
  <font color="#0000A0">Private</font> <font color="#0000A0">Declare</font> <font color="#0000A0">Function</font> SQLFreeHandle <font color="#0000A0">Lib</font> "odbc32.dll" _
       (ByVal HandleType <font color="#0000A0">As</font> Integer, _
       <font color="#0000A0">ByVal</font> Handle <font color="#0000A0">As</font> Long) <font color="#0000A0">As</font> <font color="#0000A0">Long</font>
  
  <font color="#0000A0">Private</font> <font color="#0000A0">Const</font> DSN_LEN <font color="#0000A0">As</font> <font color="#0000A0">Long</font> = 32
  <font color="#0000A0">Private</font> <font color="#0000A0">Const</font> DESC_LENGTH <font color="#0000A0">As</font> <font color="#0000A0">Long</font> = 128
  <font color="#0000A0">Private</font> <font color="#0000A0">Const</font> FoundOk <font color="#0000A0">As</font> <font color="#0000A0">Long</font> = 0
  <font color="#0000A0">Private</font> <font color="#0000A0">Const</font> GetNext <font color="#0000A0">As</font> <font color="#0000A0">Long</font> = 1
  <font color="#0000A0">Private</font> <font color="#0000A0">Const</font> Null_HANDLE <font color="#0000A0">As</font> <font color="#0000A0">Long</font> = 0
  <font color="#0000A0">Private</font> <font color="#0000A0">Const</font> HANDLE_ENV <font color="#0000A0">As</font> <font color="#0000A0">Long</font> = 1
  <font color="#0000A0">Private</font> <font color="#0000A0">Const</font> ATTR_ODBC_VERSION <font color="#0000A0">As</font> <font color="#0000A0">Long</font> = 200
  <font color="#0000A0">Private</font> <font color="#0000A0">Const</font> OV_ODBC3 <font color="#0000A0">As</font> <font color="#0000A0">Long</font> = 3
  <font color="#0000A0">Private</font> <font color="#0000A0">Const</font> IS_INTEGER <font color="#0000A0">As</font> <font color="#0000A0">Long</font> = (-6)
  <font color="#0000A0">Private</font> UserSysStringArray() <font color="#0000A0">As</font> <font color="#0000A0">String</font>
  
  <font color="#0000A0">Sub</font> UserSysDSNs()
  
       <font color="#0000A0">Dim</font> hEnv <font color="#0000A0">As</font> <font color="#0000A0">Long</font>
       <font color="#0000A0">Dim</font> sServer <font color="#0000A0">As</font> <font color="#0000A0">String</font>
       <font color="#0000A0">Dim</font> sDriver <font color="#0000A0">As</font> <font color="#0000A0">String</font>
       <font color="#0000A0">Dim</font> nSvrLen <font color="#0000A0">As</font> <font color="#0000A0">Integer</font>
       <font color="#0000A0">Dim</font> nDvrLen <font color="#0000A0">As</font> <font color="#0000A0">Integer</font>
       <font color="#0000A0">Dim</font> ArrayCntr <font color="#0000A0">As</font> <font color="#0000A0">Integer</font>
       ArrayCntr = 0
      
      
      
       <font color="#0000A0">If</font> SQLAllocHandle(HANDLE_ENV, _
           Null_HANDLE, hEnv) <> 0 <font color="#0000A0">Then</font>
      
           <font color="#0000A0">If</font> SQLSetEnvAttr(hEnv, _
               ATTR_ODBC_VERSION, _
               OV_ODBC3, _
               IS_INTEGER) <> 0 <font color="#0000A0">Then</font>
      
                   sServer = Space$(DSN_LEN)
                   sDriver = Space$(DESC_LENGTH)
      
                   <font color="#0000A0">Do</font> <font color="#0000A0">While</font> SQLDataSources(hEnv, _
                       GetNext, _
                       sServer, _
                       DSN_LEN, _
                       nSvrLen, _
                       sDriver, _
                       DESC_LENGTH, _
                       nDvrLen) = FoundOk
                      
                       ArrayCntr = ArrayCntr + 1
                       <font color="#0000A0">ReDim</font> <font color="#0000A0">Preserve</font> UserSysStringArray(1 <font color="#0000A0">To</font> ArrayCntr)
                       UserSysStringArray(ArrayCntr) = Left$(sServer, nSvrLen)
                       sServer = Space$(DSN_LEN)
                   <font color="#0000A0">Loop</font>
           <font color="#0000A0">End</font> <font color="#0000A0">If</font>
           <font color="#0000A0">Call</font> SQLFreeHandle(HANDLE_ENV, hEnv)
       <font color="#0000A0">End</font> <font color="#0000A0">If</font>
      
       ArrayCntr = 0
      
       <font color="#0000A0">Do</font> <font color="#0000A0">Until</font> ArrayCntr = UBound(UserSysStringArray)
           ArrayCntr = ArrayCntr + 1
           Sheet1.Range("A" & ArrayCntr).Value = UserSysStringArray(ArrayCntr)
       <font color="#0000A0">Loop</font>
  
  <font color="#0000A0">End</font> <font color="#0000A0">Sub</font>
  
</FONT></td></tr></table>
 
Upvote 0
P.S.
I'm assuming that your are fairly familiar with VBA. If not let me know. Seeing that you have went through so much trouble, I tested this code on two machines. Win ME and W2k. And then cross referenced the list produced with that in the registry. Both were correct. Hope it helps you.
I did not have any problem with the API as part of the user group(Microsoft's Default settings for the user group) in Win2k.
Tom
 
Upvote 0
Your code posted above was excellent and was just what I needed. Is there a way to tell whether a given DSN is a User, System or File DSN using API calls? Thanks!
 
Upvote 0
Code:
Option Explicit

Private Declare Function SQLDataSources Lib "odbc32.dll" (ByVal hEnv As Long, ByVal fDirection As Integer, ByVal szDSN As String, ByVal cbDSNMax As Integer, pcbDSN As Integer, ByVal szDescription As String, ByVal cbDescriptionMax As Integer, pcbDescription As Integer) As Long
Private Declare Function SQLAllocHandle Lib "odbc32.dll" (ByVal HandleType As Integer, ByVal InputHandle As Long, OutputHandlePtr As Long) As Long
Private Declare Function SQLSetEnvAttr Lib "odbc32.dll" (ByVal EnvironmentHandle As Long, ByVal dwAttribute As Long, ByVal ValuePtr As Long, ByVal StringLen As Long) As Long
Private Declare Function SQLFreeHandle Lib "odbc32.dll" (ByVal HandleType As Integer, ByVal Handle As Long) As Long

Private Const DSN_LEN As Long = 32
Private Const DESC_LENGTH As Long = 128
Private Const FoundOk As Long = 0
Private Const GetNext As Long = 1
Private Const Null_HANDLE As Long = 0
Private Const HANDLE_ENV As Long = 1
Private Const ATTR_ODBC_VERSION As Long = 200
Private Const OV_ODBC3 As Long = 3
Private Const IS_INTEGER As Long = (-6)

Sub Example()
    If DSNExists("MS Access Database") Then
        MsgBox "DSN Exists"
    Else
        MsgBox "DSN does not exist"
    End If
End Sub

Function DSNExists(DSN As String) As Boolean

 Dim hEnv As Long
 Dim sServer As String
 Dim sDriver As String
 Dim nSvrLen As Integer
 Dim nDvrLen As Integer
 
 If SQLAllocHandle(HANDLE_ENV, _
     Null_HANDLE, hEnv) <> 0 Then
    
     If SQLSetEnvAttr(hEnv, _
         ATTR_ODBC_VERSION, _
         OV_ODBC3, _
         IS_INTEGER) <> 0 Then
        
         sServer = Space$(DSN_LEN)
         sDriver = Space$(DESC_LENGTH)
        
         Do While SQLDataSources(hEnv, _
             GetNext, _
             sServer, _
             DSN_LEN, _
             nSvrLen, _
             sDriver, _
             DESC_LENGTH, _
             nDvrLen) = FoundOk
             If Trim(DSN) = Trim(Left$(sServer, nSvrLen)) Then
                DSNExists = True
                Exit Do
             End If
             sServer = Space$(DSN_LEN)
         Loop
     End If
     Call SQLFreeHandle(HANDLE_ENV, hEnv)
 End If
End Function
 
Upvote 0

Forum statistics

Threads
1,214,819
Messages
6,121,739
Members
449,050
Latest member
excelknuckles

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top