Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 418
  • Last Modified:

checking for a specific ODBC connection

I need a way to check for an existing ODBC connection in the odbc data source administrator.  All the research I have done indicates that the values are stored in either the INI file (not in XP) or the registry.  I have determined that the ini file does not exist and I do not have access to the registry on this computer so my search skills are limited here.  What I would like an easy way to see if the the connection named "tempconn" exists in the registry so that my code does not update the connection information when I use RegisterDatabase to establish the missing connection.  This is all being done on an xp system (soon to be vista) inside a ms access database.
Joe Overman
Joe Overman
1 Solution
What code are you currently using, r u using the sqlconfig... api? if so then could you not try to open as edit. If it failed then try to add on. If that fails then summat is knackered or it already exists!
Seems like you would be able to read the registry if you can add the connection to the registry with the registerdatabase command.  The function will return true if connection name exists.
Function ConnectionExists(sConnectionName As String) As Boolean
Const HKEY_CURRENT_USER = &H80000001
Dim objReg As Object
Dim strComputer, strkeypath As String
Dim arrValueNames As Variant
Dim subkey As Variant
strComputer = "."
Set objReg = GetObject("winmgmts:\\" & strComputer & "\root\default:StdRegProv")
strkeypath = "Software\ODBC\ODBC.ini"
objReg.enumkey HKEY_CURRENT_USER, strkeypath, arrValueNames
For Each subkey In arrValueNames
    If sConnectionName = subkey Then
        ConnectionExists = True
    End If
End Function

Open in new window

Gustav BrockCIOCommented:
> the ini file does not exist and I do not have access to the registry on this computer ..

Then you are left with trial and error.
Open a connection assuming your DSN exists. Catch an error.
If no error is catched, the DSN exists.

Joe OvermanEngineerAuthor Commented:
That is just what I need.  Thanks

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now