Thread: DSN Listing
View Single Post
  #3   Report Post  
Posted to microsoft.public.excel.programming
RB Smissaert RB Smissaert is offline
external usenet poster
 
Posts: 2,452
Default 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