Kirk
Sep 26th, 2000, 12:17 PM
Hi:
I would like to check for a DSN in the registry. I've searched through the relevant articles and posts here but I still don't get how to do it.
I've tried using RegQueryValue which seems to be close to what I want. I snagged the following to try to retrieve the name of the database. What I'd really like to do is find out if the datasource is there.
Dim hKey As Long ' receives a handle to the newly created or opened registry key
Dim subkey As String ' name of the subkey to open
Dim stringbuffer As String ' receives data read from the registry
Dim datatype As Long ' receives data type of read value
Dim slength As Long ' receives length of returned data
Dim retval As Long ' return value
' Set the name of the new key and the default security settings
subkey = "Software\ODBC\ODBC.INI"
' Create or open the registry key
retval = RegOpenKeyEx(HKEY_LOCAL_MACHINE, subkey, 0, KEY_READ, hKey)
If retval <> 0 Then
Debug.Print "ERROR: Unable to open registry key!"
Exit Sub
End If
' Make room in the buffer to receive the incoming data.
stringbuffer = Space(255)
slength = 255
' Read the "username" value from the registry key.
retval = RegQueryValueEx(hKey, "DataSourceName", 0, datatype, ByVal stringbuffer, slength)
' Only attempt to display the data if it is in fact a string.
If datatype = REG_SZ Then
' Remove empty space from the buffer and display the result.
stringbuffer = Left(stringbuffer, slength)
Debug.Print "Database: "; stringbuffer
Else
' Don't bother trying to read any other data types.
Debug.Print "Data not in string format. Unable to interpret data."
End If
' Close the registry key.
retval = RegCloseKey(hKey)
End Sub
(Snagged from http://209.207.250.135/ref/r/regqueryvalueex.html)
I would like to check for a DSN in the registry. I've searched through the relevant articles and posts here but I still don't get how to do it.
I've tried using RegQueryValue which seems to be close to what I want. I snagged the following to try to retrieve the name of the database. What I'd really like to do is find out if the datasource is there.
Dim hKey As Long ' receives a handle to the newly created or opened registry key
Dim subkey As String ' name of the subkey to open
Dim stringbuffer As String ' receives data read from the registry
Dim datatype As Long ' receives data type of read value
Dim slength As Long ' receives length of returned data
Dim retval As Long ' return value
' Set the name of the new key and the default security settings
subkey = "Software\ODBC\ODBC.INI"
' Create or open the registry key
retval = RegOpenKeyEx(HKEY_LOCAL_MACHINE, subkey, 0, KEY_READ, hKey)
If retval <> 0 Then
Debug.Print "ERROR: Unable to open registry key!"
Exit Sub
End If
' Make room in the buffer to receive the incoming data.
stringbuffer = Space(255)
slength = 255
' Read the "username" value from the registry key.
retval = RegQueryValueEx(hKey, "DataSourceName", 0, datatype, ByVal stringbuffer, slength)
' Only attempt to display the data if it is in fact a string.
If datatype = REG_SZ Then
' Remove empty space from the buffer and display the result.
stringbuffer = Left(stringbuffer, slength)
Debug.Print "Database: "; stringbuffer
Else
' Don't bother trying to read any other data types.
Debug.Print "Data not in string format. Unable to interpret data."
End If
' Close the registry key.
retval = RegCloseKey(hKey)
End Sub
(Snagged from http://209.207.250.135/ref/r/regqueryvalueex.html)