Results 1 to 5 of 5

Thread: Creating DSN in code

  1. #1

    Thread Starter
    Fanatic Member Stevie's Avatar
    Join Date
    Mar 2000
    Location
    London, UK
    Posts
    565
    I have to amend one of my projects so that it uses ADO instead of DAO. I need to connect via a DSN, but I want to create it in code. I usually do it with DBEngine.RegisterDatabase, but is there another way I can do it so that I do not have to add DAO references into my project?

    Also how do I check if a DSN exists (apart from trying to connect to the DB and testing if it has suceeded)?

    All help greatly appreciated!!

  2. #2
    Lively Member
    Join Date
    Dec 1999
    Location
    Karlsruhe, Germany
    Posts
    122
    I usually work with data link files (right click in explorer, new, Microsoft data link).

    The connect string for a data link file is

    File Name=datalinkfile

    Roger

  3. #3

    Thread Starter
    Fanatic Member Stevie's Avatar
    Join Date
    Mar 2000
    Location
    London, UK
    Posts
    565
    I've managed to create a dsn by using the SqlConfigDataSource API (I found it in an earlier post), but can anyone tell me how to check if a dsn already exists?

  4. #4
    Serge's Avatar
    Join Date
    Feb 1999
    Location
    Scottsdale, Arizona, USA
    Posts
    2,744
    You can use this function I wrote. Put this in a module:
    Code:
    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 RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
    Private Const HKEY_LOCAL_MACHINE = &H80000002
    Public Const HKEY_CURRENT_USER = &H80000001
    Private Const ERROR_SUCCESS = 0&
    Private Const KEY_QUERY_VALUE = &H1
    Private Const KEY_NOTIFY = &H10
    Private Const READ_CONTROL = &H20000
    Private Const KEY_ENUMERATE_SUB_KEYS = &H8
    Private Const STANDARD_RIGHTS_READ = (READ_CONTROL)
    Private Const SYNCHRONIZE = &H100000
    Private Const KEY_READ = ((STANDARD_RIGHTS_READ Or KEY_QUERY_VALUE Or KEY_ENUMERATE_SUB_KEYS Or KEY_NOTIFY) And (Not SYNCHRONIZE))
    Public Enum enumDSNType
        eUser = 1
        eSystem = 2
    End Enum
    Public Function IsDSNExist(pDSNName As String, pType As enumDSNType) As Boolean
        Dim lKeyHandle As Long
        Dim lRet As Long
        Dim strBuffer As String
        Dim strKey As String
        Dim strValue As String
        
        strKey = "SOFTWARE\ODBC\ODBC.INI\" & pDSNName
        
        If pType = eSystem Then
            lRet = RegOpenKeyEx(HKEY_LOCAL_MACHINE, strKey, 0, KEY_READ, lKeyHandle)
        Else
            lRet = RegOpenKeyEx(HKEY_CURRENT_USER, strKey, 0, KEY_READ, lKeyHandle)
        End If
        If lRet = ERROR_SUCCESS Then
            IsDSNExist = True
            RegCloseKey lKeyHandle
        End If
    End Function
    Then you can use it like this:


    MsgBox IsDSNExist("MyDSNName", eSystem)

  5. #5

    Thread Starter
    Fanatic Member Stevie's Avatar
    Join Date
    Mar 2000
    Location
    London, UK
    Posts
    565
    Cheers Serge, thats just what I needed.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width