ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   DSN Listing (https://www.excelbanter.com/excel-programming/324001-dsn-listing.html)

Michael Rekas[_2_]

DSN Listing
 
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

RB Smissaert

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

RBS

"Michael Rekas" wrote in message
...
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



RB Smissaert

DSN Listing
 
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


"Michael Rekas" wrote in message
...
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



RB Smissaert

DSN Listing
 
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


"Michael Rekas" wrote in message
...
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



Michael Rekas[_2_]

DSN Listing
 
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.



On Fri, 25 Feb 2005 14:45:34 -0000, "RB Smissaert"
wrote:

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


"Michael Rekas" wrote in message
.. .
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



RB Smissaert

DSN Listing
 
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


"Michael Rekas" wrote in message
...
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.



On Fri, 25 Feb 2005 14:45:34 -0000, "RB Smissaert"
wrote:

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


"Michael Rekas" wrote in message
. ..
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




RB Smissaert

DSN Listing
 
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


"Michael Rekas" wrote in message
...
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.



On Fri, 25 Feb 2005 14:45:34 -0000, "RB Smissaert"
wrote:

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


"Michael Rekas" wrote in message
. ..
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





All times are GMT +1. The time now is 08:05 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com