Create a new User Dsn in VB6

Posted on 2009-02-11
Last Modified: 2013-12-25
I have an old program in VB6 that creates a new System Dsn if it does not exist on the user's PC. Now the management has decided that only User Dsn is allowed.
I have not found any way to change my code to get a User Dsn instead of a System Dsn.
'Create the new DSN key.



      DataSourceName, 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, "LastUser", 0&, REG_SZ, _

      ByVal LastUser, Len(LastUser))

 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.

 'Specify the new value.

 'Close the key.

 lResult = RegCreateKey(HKEY_LOCAL_MACHINE, _

      "SOFTWARE\ODBC\ODBC.INI\ODBC Data Sources", hKeyHandle)

 lResult = RegSetValueEx(hKeyHandle, DataSourceName, 0&, REG_SZ, _

      ByVal DriverName, Len(DriverName))

 lResult = RegCloseKey(hKeyHandle)

Open in new window

Question by:LouisaMaria
    LVL 32

    Accepted Solution

    1/ You need to use HKEY_CURRENT_USER

    2/  MSDN quote... RegCreateKey Function Creates the specified registry key. If the key already exists in the registry, the function opens it.<!--Content type: HTML. Transform: webcollection2mtps.xslt.--><!---->
    Note  This function is provided only for compatibility with 16-bit versions of Windows. Applications should use the RegCreateKeyEx Function

    see for an example using RegCreateKeyEx that works on my machine when I modify your code. Note that I had to put all of the example declares into a module to get it to compile.

    couldnt use all you code as dont have the data for driver etc so i used some test strings for databasename and descript.


    Author Comment

    Thanks, this was just the information I needed to solve it.

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    VB6 Rich TextBox command works only in IDE 8 26
    Port V2 16 35
    Public Sub, Empty Cell 7 52
    How to debug this code 7 30
    The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
    Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
    Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
    This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now