shubbell
asked on
Reading Registry in VB6
I would like to see if an ODBC DSN has been created from my VB program. So I need to read the registry to see if it has been created? Any help is greatly appreciated.
ASKER
emoreau, maybe I'm not clear. I want to check to see if a particular OBDC DSN entry has been created from inside my VB program. If it has not, then i must tell the user to quit my VB program and go create it and then start the VB program up again.
Why ask your user to create a DSN when your application can create one (with RegisterDatabase)?
ASKER
emoreau, I have no idea. Seems stupid to me too. But don't you think I need to check to see if it exists before I write over it with RegisterDatabase?
As I already said, it is so fast registering it (less than a second) that I don't check if it already exist even if I will overwrite it with the same value.
By overwriting it, I am sure that my DSN is always as I want it to be!
By overwriting it, I am sure that my DSN is always as I want it to be!
ASKER
emoreau, I really need to check it before writing it with RegisterDatabase. I do not trust my users to name it the same thing everytime and other programs depend on this particular OBDC DSN. Do you know of anyway to check it first? Thank you.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
manojamin, that is more of what I was looking for, however, i can not get it to give me the correct answer. In Function IsExistKey "R" always has a value of 2 whether the DSN exists or not. Any clues? Thank you so much.
after which call?
R = RegOpenKey(hInKey, subkey$, hSubKey)
or
R = RegQueryValueEx(hSubKey, valname$, 0, dwType, ByVal v$, SZ)
R = RegOpenKey(hInKey, subkey$, hSubKey)
or
R = RegQueryValueEx(hSubKey, valname$, 0, dwType, ByVal v$, SZ)
ASKER
manojamin, after
R = RegQueryValueEx(hSubKey, valname$, 0, dwType, ByVal v$, SZ). It always returns 2 which after formating the message says "Unable to find file specified".
R = RegQueryValueEx(hSubKey, valname$, 0, dwType, ByVal v$, SZ). It always returns 2 which after formating the message says "Unable to find file specified".
ASKER
manojamin, please disregard my stupidity. It is after
R = RegOpenKey(hInKey, subkey$, hSubKey) that i get the value of 2. THanks.
R = RegOpenKey(hInKey, subkey$, hSubKey) that i get the value of 2. THanks.
'forget about that, if it does not work, use this
Private Const SQL_FETCH_NEXT As Long = 1
Private Declare Function SQLAllocEnv Lib "odbc32.dll" (phenv&) As Integer
Private Declare Function SQLDataSources Lib "odbc32.dll" (ByVal henv&, ByVal fDirection%, ByVal szDSN$, ByVal cbDSNMax%, pcbDSN%, ByVal szDescription$, ByVal cbDescriptionMax%, pcbDescription%) As Integer
Sub GetDSNsAndDrivers()
On Error Resume Next
Dim i As Integer
Dim sDSNItem As String * 1023
Dim sDRVItem As String * 1023
Dim sDSN As String
Dim sDRV As String
Dim iDSNLen As Integer
Dim iDRVLen As Integer
Dim lHenv As Long 'handle to the environment
'get the DSNs
If SQLAllocEnv(lHenv) <> -1 Then
Do Until i <> SQL_SUCCESS
sDSNItem = Space(1024)
sDRVItem = Space(1024)
i = SQLDataSources(lHenv, SQL_FETCH_NEXT, sDSNItem, 1024, iDSNLen, sDRVItem, 1024, iDRVLen)
sDSN = VBA.Left(sDSNItem, iDSNLen)
sDRV = VBA.Left(sDRVItem, iDRVLen)
Debug.Print sDSN
Loop
End If
End Sub
'this will print all the system dsn in debug window...
Private Const SQL_FETCH_NEXT As Long = 1
Private Declare Function SQLAllocEnv Lib "odbc32.dll" (phenv&) As Integer
Private Declare Function SQLDataSources Lib "odbc32.dll" (ByVal henv&, ByVal fDirection%, ByVal szDSN$, ByVal cbDSNMax%, pcbDSN%, ByVal szDescription$, ByVal cbDescriptionMax%, pcbDescription%) As Integer
Sub GetDSNsAndDrivers()
On Error Resume Next
Dim i As Integer
Dim sDSNItem As String * 1023
Dim sDRVItem As String * 1023
Dim sDSN As String
Dim sDRV As String
Dim iDSNLen As Integer
Dim iDRVLen As Integer
Dim lHenv As Long 'handle to the environment
'get the DSNs
If SQLAllocEnv(lHenv) <> -1 Then
Do Until i <> SQL_SUCCESS
sDSNItem = Space(1024)
sDRVItem = Space(1024)
i = SQLDataSources(lHenv, SQL_FETCH_NEXT, sDSNItem, 1024, iDSNLen, sDRVItem, 1024, iDRVLen)
sDSN = VBA.Left(sDSNItem, iDSNLen)
sDRV = VBA.Left(sDRVItem, iDRVLen)
Debug.Print sDSN
Loop
End If
End Sub
'this will print all the system dsn in debug window...
make this as
Dim sDSNItem As String * 1023
Dim sDRVItem As String * 1023
not
Dim sDSNItem As String * 1024
Dim sDRVItem As String * 1024
does not really make difference though...
Dim sDSNItem As String * 1023
Dim sDRVItem As String * 1023
not
Dim sDSNItem As String * 1024
Dim sDRVItem As String * 1024
does not really make difference though...
Oh dear God! another mistake!!!!
make this as
Dim sDSNItem As String * 1024
Dim sDRVItem As String * 1024
not
Dim sDSNItem As String * 1023
Dim sDRVItem As String * 1023
does not really make difference though
make this as
Dim sDSNItem As String * 1024
Dim sDRVItem As String * 1024
not
Dim sDSNItem As String * 1023
Dim sDRVItem As String * 1023
does not really make difference though
ASKER
thank you
It is so quick to recreate that I don't loose time checking for existence!
To recreate it, I use the RegisterDatabase method (of DAO) or rdoRegisterDatabase (of RDO).