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: 395
  • Last Modified:

Create a new User Dsn in VB6

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

1 Solution
Robberbaron (robr)Commented:
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 http://www.xs4all.nl/~rjg70/vbapi/ref/r/regcreatekeyex.html 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.

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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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