Hi,
I need to create a DSN from my vb app. This I've managed. What I haven't figured out is how to check if a DSN of that name already exists. How can this be done. Any help will be appreciated.
Thanks,
Rammy.
Printable View
Hi,
I need to create a DSN from my vb app. This I've managed. What I haven't figured out is how to check if a DSN of that name already exists. How can this be done. Any help will be appreciated.
Thanks,
Rammy.
Although it's not what you asked, the best answer is not to use a DSN since then you don't have to rely on it being setup properly on the PC. If you specify the driver, server and database you can avoid using DSNs at all.. eg;
MyConnection.Open "Provider=SQLOLEDB;Driver={SQL Server};Server=MYSERVER;Database=MYDB;UID=SA;PWD=;"
will open the connection without being told a DSN.
The other thing is once you've got a DSN set up on a PC the user could then use your DSN to access the data without your program - which could be a security risk.
Try the following :
Hope this helps :)Code:Public Function DSNExist(strDSNName As String) As Boolean
'***************************************************************************
'Purpose: Determines whether a DSN already exists.
'Parameters: strDSNName - DSN Name to be checked for.
'Returns: True/False - DSN Exists.
'***************************************************************************
On Error GoTo ErrorHandler
Dim lngKeyHandle As Long
Dim lngReturn As Long
Dim strKey As String
DSNExist = False
strKey = "SOFTWARE\ODBC\ODBC.INI\" & strDSNName
' Open registry key
lngReturn = RegOpenKeyEx(HKEY_CURRENT_USER, strKey, 0, KEY_READ, lngKeyHandle)
If lngReturn = ERROR_SUCCESS Then
DSNExist = True
' Close registry key
RegCloseKey lngKeyHandle
End If
End Function