Wrox Home  
Search P2P Archive for: Go

  Return to Index  

pro_vb thread: creating dsn through vb code


Message #1 by "suresh babu" <mvvsuri@r...> on Mon, 13 Jan 2003 11:31:39
Put the following in a module:

Option Explicit

Private Const REG_SZ = 1    'Constant for a string variable type.
Private Const HKEY_LOCAL_MACHINE = &H80000002

Private Declare Function RegCreateKey Lib "advapi32.dll" Alias
"RegCreateKeyA" ( _
    ByVal hKey As Long, _
    ByVal lpSubKey As String, _
    phkResult As Long) As Long

Private Declare Function RegSetValueEx Lib "advapi32.dll" Alias
"RegSetValueExA" ( _
    ByVal hKey As Long, _
    ByVal lpValueName As String, _
    ByVal Reserved As Long, _
    ByVal dwType As Long, _
    lpData As Any, _
    ByVal cbData As Long) As Long

Private Declare Function RegCloseKey Lib "advapi32.dll" ( _
    ByVal hKey As Long) As Long

Public Sub MakeDSN( _
    ByVal DSNName As String, _
    ByVal DatabaseName As String, _
    ByVal Description As String, _
    ByVal DriverName As String, _
    ByVal DriverPath As String, _
    ByVal UserName As String, _
    ByVal Server As String)

Dim lResult             As Long
Dim hKeyHandle          As Long

    'Create the new DSN key.
    lResult = RegCreateKey(HKEY_LOCAL_MACHINE, "SOFTWARE\ODBC\ODBC.INI\" & _
         DSNName, hKeyHandle)

    'Set the values of the new DSN key.
    lResult = RegSetValueEx(hKeyHandle, "Database", 0&, REG_SZ, _
       ByVal DatabaseName, Len(DatabaseName))
    lResult = RegSetValueEx(hKeyHandle, "Description", 0&, REG_SZ, _
       ByVal Description, Len(Description))
    lResult = RegSetValueEx(hKeyHandle, "Driver", 0&, REG_SZ, _
       ByVal DriverPath, Len(DriverPath))
    lResult = RegSetValueEx(hKeyHandle, "UserName", 0&, REG_SZ, _
       ByVal UserName, Len(UserName))
    lResult = RegSetValueEx(hKeyHandle, "Server", 0&, REG_SZ, _
       ByVal Server, Len(Server))

    'Close the new DSN key.
    lResult = RegCloseKey(hKeyHandle)

    'Open ODBC Data Sources key to list the new DSN in the ODBC Manager.
    lResult = RegCreateKey(HKEY_LOCAL_MACHINE, _
       "SOFTWARE\ODBC\ODBC.INI\ODBC Data Sources", hKeyHandle)

    'Specify the new value.
    lResult = RegSetValueEx(hKeyHandle, DSNName, 0&, REG_SZ, _
       ByVal DriverName, Len(DriverName))

    'Close the key.
    lResult = RegCloseKey(hKeyHandle)

End Sub

Call the sub with code like this:

    Call MakeDSN( _
        "pkipetest", _
        "cpms", _
        "test odbc driver", _
        "SQL Server", _
        "C:\WINNT\System32\sqlsrv32.dll", _
        "pkipe1", _
        "cdssvr02")

Good luck!

Pete


-----Original Message-----
From: suresh babu [mailto:mvvsuri@r...]
Sent: Monday, January 13, 2003 11:32 AM
To: professional vb
Subject: [pro_vb] creating dsn through vb code


greetings,
   how can i make dsn through vb code to connect to ms-access or any other
databases.
   what i need:
  when i run the vb program that dsn automatically created. not through
the controle pannel.it through vb coding ineed to create dsn.

any one help me.
than q,
suresh



  Return to Index