Hi all
I want to get a list of ODBC connections, and then pick one - the
point is, that the connection I am looking for might be spelled
differently (e.g. with space). So with a list I can find it eaily.
Any suggestions?
Sonnich
This almost works - it gets the amount, but data is empty. Anybody
knows why?
Private Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias
"RegOpenKeyExA" ( _
ByVal hKey As Long, _
ByVal lpSubKey As String, _
ByVal ulOptions As Long, _
ByVal samDesired As Long, _
phkResult As Long) As Long
Private Declare Function RegQueryInfoKey Lib "advapi32.dll" Alias
"RegQueryInfoKeyA" ( _
ByVal hKey As Long, _
ByVal lpClass As String, _
lpcbClass As Long, _
ByVal lpReserved As Long, _
lpcSubKeys As Long, _
lpcbMaxSubKeyLen As Long, _
lpcbMaxClassLen As Long, _
lpcValues As Long, _
lpcbMaxValueNameLen As Long, _
lpcbMaxValueLen As Long, _
lpcbSecurityDescriptor As Long, _
lpftLastWriteTime As Long) As Long
Private Declare Function RegEnumValue Lib "advapi32.dll" Alias
"RegEnumValueA" ( _
ByVal hKey As Long, _
ByVal dwIndex As Long, _
ByVal lpValueName As String, _
lpcbValueName As Long, _
ByVal lpReserved As Long, _
lpType As Long, _
ByVal lpData As String, _
lpcbData As Long) As Long
Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As
Long) As Long
Private Declare Function RegQueryValueExString Lib "advapi32.dll"
Alias "RegQueryValueExA" ( _
ByVal hKey As Long, _
ByVal lpValueName As String, _
ByVal lpReserved As Long, _
lpType As Long, _
ByVal lpData As String, _
lpcbData As Long) As Long
' Registry value type definitions
Private Const REG_NONE As Long = 0
Private Const REG_SZ As Long = 1
Private Const REG_EXPAND_SZ As Long = 2
Private Const REG_BINARY As Long = 3
Private Const REG_DWORD As Long = 4
Private Const REG_LINK As Long = 6
Private Const REG_MULTI_SZ As Long = 7
Private Const REG_RESOURCE_LIST As Long = 8
Private Sub CommandButton1_Click()
Dim mCurrentKey As Long
Result = RegOpenKeyEx(&H80000002, "SOFTWARE\ODBC\ODBC.INI\ODBC Data
Sources", 0&, &H2001D, mCurrentKey)
Dim DataType As Long
Dim Value As String
Dim ValueLength As Long
Dim ReadString As String
' Dim Result As Long
If False Then // this works
Result = RegQueryValueExString(mCurrentKey, "ChinaWise", 0&,
DataType, vbNullString, ValueLength)
If Result = ERROR_SUCCESS Then
Value = Space(ValueLength)
Result = RegQueryValueExString(mCurrentKey, "ChinaWise", 0&,
DataType, Value, ValueLength)
If Result = ERROR_SUCCESS Then
Select Case DataType
Case REG_SZ, REG_EXPAND_SZ, REG_MULTI_SZ
ReadString = Left(Value, ValueLength - 1)
Case Else
Err.Raise vbObjectError + 515, , "Not a string value: " &
Name
End Select
End If
End If
If Result <> ERROR_SUCCESS Then
Err.Raise vbObjectError + 515, , "Cannot read string value: " &
Name
End If
End If
Dim Values()
ReDim Preserve Values(0)
' Dim Name As String
Dim NameLength As Long
Dim ValueCount As Long
Dim MaxValueLength As Long
Dim i As Long
If RegQueryInfoKey(mCurrentKey, vbNullString, 0&, 0&, 0&, 0&, 0&,
ValueCount, MaxValueLength, 0&, 0&, 0&) = ERROR_SUCCESS Then
If ValueCount > 0 Then
ReDim Values(0 To ValueCount - 1)
Else
Values = Split("")
End If
MaxValueLength = MaxValueLength + 1
sName = Space(MaxValueLength) // always empty - why?
For i = 0 To ValueCount - 1
NameLength = MaxValueLength
If RegEnumValue(mCurrentKey, i, sName, NameLength, 0&, 0&,
vbNullString, 0&) = ERROR_SUCCESS Then
Values(i) = Left(sName, NameLength)
Else
Err.Raise vbObjectError + 520, , "Error reading value name"
End If
Next
Else
Err.Raise vbObjectError + 521, , "Error reading value names"
End If
RegCloseKey (mCurrentKey)
'GetValueNames (names)
For i = 0 To UBound(Values)
Cells(1 + i, 1) = i
Cells(1 + i, 2) = Values(i)
Next
End Sub