DSN Listing

  • Thread starter Thread starter Michael Rekas
  • Start date Start date
M

Michael Rekas

Hi there

Does anybody have code that will bring in a list of external data
sources i.e. the list that appears when you click Data, Get external
Data, New Database Query.

Thanks

Michael
 
One way (the only one?) is to read it from the registry:
HKEY_USERS\bla-bla-bla\Software\ODBC\ODBC.INI

RBS
 
This code will dump the external datasources in the sheet on my machine.
All you will have to figure out is how to get the string:
"S-1-5-21-45121442-3062903995-4062188013-1005"
As I take it that this will be different on every PC.
Maybe somebody could explain how to do this.


Option Explicit
Const ERROR_NO_MORE_ITEMS = 259&
Const HKEY_USERS = &H80000003
Private Declare Function RegCloseKey _
Lib "advapi32.dll" _
(ByVal hKey As Long) As Long
Private Declare Function RegOpenKey _
Lib "advapi32.dll" _
Alias "RegOpenKeyA" (ByVal hKey As Long, _
ByVal lpSubKey As String, _
phkResult As Long) As Long
Private Declare Function RegEnumKeyEx _
Lib "advapi32.dll" Alias _
"RegEnumKeyExA" (ByVal hKey As Long, _
ByVal dwIndex As Long, _
ByVal lpName As String, _
lpcbName As Long, _
ByVal lpReserved As Long, _
ByVal lpClass As String, _
lpcbClass As Long, _
lpftLastWriteTime As Any) As Long

Sub ShowExternalDataSources()

Dim hKey As Long
Dim Cnt As Long
Dim sName As String
Dim sData As String
Dim Ret As Long
Dim RetData As Long
Const BUFFER_SIZE As Long = 255

Ret = BUFFER_SIZE

If RegOpenKey(HKEY_USERS, _
"S-1-5-21-45121442-3062903995-4062188013-1005" & _
"\Software\ODBC\ODBC.INI", _
hKey) = 0 Then
sName = Space(BUFFER_SIZE)
While RegEnumKeyEx(hKey, _
Cnt, _
sName, _
Ret, _
ByVal 0&, _
vbNullString, _
ByVal 0&, _
ByVal 0&) <> ERROR_NO_MORE_ITEMS
Cnt = Cnt + 1
Cells(Cnt, 1) = Left$(sName, Ret)
sName = Space(BUFFER_SIZE)
Ret = BUFFER_SIZE
Wend
'close the registry key
RegCloseKey hKey
Else
MsgBox "Error while calling RegOpenKey", , ""
End If

End Sub


RBS
 
OK, this code will do it:


Option Explicit
Const ERROR_NO_MORE_ITEMS = 259&
Const HKEY_CURRENT_USER = &H80000001
Const HKEY_USERS = &H80000003
Private Declare Function RegCloseKey _
Lib "advapi32.dll" _
(ByVal hKey As Long) As Long
Private Declare Function RegOpenKey _
Lib "advapi32.dll" _
Alias "RegOpenKeyA" (ByVal hKey As Long, _
ByVal lpSubKey As String, _
phkResult As Long) As Long
Private Declare Function RegEnumKeyEx _
Lib "advapi32.dll" Alias _
"RegEnumKeyExA" (ByVal hKey As Long, _
ByVal dwIndex As Long, _
ByVal lpName As String, _
lpcbName As Long, _
ByVal lpReserved As Long, _
ByVal lpClass As String, _
lpcbClass As Long, _
lpftLastWriteTime As Any) As Long

Sub ShowExternalDataSources()

Dim hKey As Long
Dim Cnt As Long
Dim sName As String
Dim sData As String
Dim Ret As Long
Dim RetData As Long
Const BUFFER_SIZE As Long = 255

Ret = BUFFER_SIZE

If RegOpenKey(HKEY_CURRENT_USER, _
"Software\ODBC\ODBC.INI", _
hKey) = 0 Then
sName = Space(BUFFER_SIZE)
While RegEnumKeyEx(hKey, _
Cnt, _
sName, _
Ret, _
ByVal 0&, _
vbNullString, _
ByVal 0&, _
ByVal 0&) <> ERROR_NO_MORE_ITEMS
Cnt = Cnt + 1
Cells(Cnt, 1) = Left$(sName, Ret)
sName = Space(BUFFER_SIZE)
Ret = BUFFER_SIZE
Wend
'close the registry key
RegCloseKey hKey
Else
MsgBox "Error while calling RegOpenKey", , ""
End If

End Sub


RBS
 
Thanks. I appreciate your persistence.

Your code returns all default and Microsoft SQL databases.

I searched the registry and found that the Pervasive.SQL DSNs (and for
that matter the Microsoft SQl DSNs) are stored in the same key under
HKEY_LOCAL_MACHINE.

I added "Const HKEY_LOCAL_MACHINE = &H80000002" to the declarations (a
good guess as I really do not understand your code!).

I also changed HKEY_CIRRENT_USER to HKEY_LOCAL_MACHINE in the Sub and
it gives me exactly what I want.

Absolutely thrilled. Thanks for the help.

Regards

Michael.
 
Nice it works now.
I have adapted the code a bit.
The collection will take care of doubles.


Option Explicit
Const ERROR_NO_MORE_ITEMS = 259&
Const HKEY_CURRENT_USER = &H80000001
Const HKEY_LOCAL_MACHINE = &H80000002
Const HKEY_USERS = &H80000003
Private Declare Function RegCloseKey _
Lib "advapi32.dll" _
(ByVal hKey As Long) As Long
Private Declare Function RegOpenKey _
Lib "advapi32.dll" _
Alias "RegOpenKeyA" (ByVal hKey As Long, _
ByVal lpSubKey As String, _
phkResult As Long) As Long
Private Declare Function RegEnumKeyEx _
Lib "advapi32.dll" Alias _
"RegEnumKeyExA" (ByVal hKey As Long, _
ByVal dwIndex As Long, _
ByVal lpName As String, _
lpcbName As Long, _
ByVal lpReserved As Long, _
ByVal lpClass As String, _
lpcbClass As Long, _
lpftLastWriteTime As Any) As Long

Sub ShowExternalDataSources()

Dim hKey As Long
Dim Cnt As Long
Dim sName As String
Dim Ret As Long
Const BUFFER_SIZE As Long = 255
Dim coExternalDataSources As Collection
Dim arrRegKeys
Dim i As Long

Set coExternalDataSources = New Collection

arrRegKeys = Array(HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE)

Ret = BUFFER_SIZE

For i = 0 To UBound(arrRegKeys)
Cnt = 0
If RegOpenKey(arrRegKeys(i), _
"Software\ODBC\ODBC.INI", _
hKey) = 0 Then
sName = Space(BUFFER_SIZE)
While RegEnumKeyEx(hKey, _
Cnt, _
sName, _
Ret, _
ByVal 0&, _
vbNullString, _
ByVal 0&, _
ByVal 0&) <> ERROR_NO_MORE_ITEMS
Cnt = Cnt + 1
On Error Resume Next
coExternalDataSources.Add Left$(sName, Ret), _
Left$(sName, Ret)
On Error GoTo 0
sName = Space(BUFFER_SIZE)
Ret = BUFFER_SIZE
Wend
'close the registry key
RegCloseKey hKey
Else
MsgBox "Error while calling RegOpenKey", , ""
End If
Next

For i = 1 To coExternalDataSources.Count
Cells(i, 1) = coExternalDataSources(i)
Next

End Sub


RBS
 
For my particular purpose I needed to see from what registry root key the
datasource came from
so I changed the code a bit. Declarations are the same.

Sub ShowExternalDataSources()

Dim hKey As Long
Dim Cnt As Long
Dim sName As String
Dim Ret As Long
Const BUFFER_SIZE As Long = 255
Dim coExternalDataSources As Collection
Dim arrRegKeys
Dim arrRegKeysNames
Dim i As Long
Dim lDataSources As Long

Set coExternalDataSources = New Collection

arrRegKeys = Array(HKEY_CURRENT_USER, _
HKEY_LOCAL_MACHINE)
arrRegKeysNames = Array("HKEY_CURRENT_USER", _
"HKEY_LOCAL_MACHINE")

Ret = BUFFER_SIZE

For i = 0 To UBound(arrRegKeys)
Cnt = 0
If RegOpenKey(arrRegKeys(i), _
"Software\ODBC\ODBC.INI", _
hKey) = 0 Then
sName = Space(BUFFER_SIZE)
While RegEnumKeyEx(hKey, _
Cnt, _
sName, _
Ret, _
ByVal 0&, _
vbNullString, _
ByVal 0&, _
ByVal 0&) <> ERROR_NO_MORE_ITEMS
Cnt = Cnt + 1
On Error Resume Next
'keep both the datasource and the registry root key
'--------------------------------------------------
coExternalDataSources.Add Array(Left$(sName, Ret), _
arrRegKeysNames(i)), _
Left$(sName, Ret)
On Error GoTo 0
sName = Space(BUFFER_SIZE)
Ret = BUFFER_SIZE
Wend
'close the registry key
RegCloseKey hKey
Else
MsgBox "Error while calling RegOpenKey", , ""
End If
Next

lDataSources = coExternalDataSources.Count

For i = 1 To lDataSources
Cells(i, 1) = coExternalDataSources(i)(0)
Cells(i, 2) = coExternalDataSources(i)(1)
Next

Range(Cells(1), Cells(lDataSources, 2)).Columns.AutoFit

End Sub


RBS
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top