We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

Create a new User Dsn in VB6

LouisaMaria
LouisaMaria asked
on
Medium Priority
412 Views
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.
        
 lResult = RegCreateKey(HKEY_LOCAL_MACHINE, "SOFTWARE\ODBC\ODBC.INI\" & _
      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

Comment
Watch Question

CERTIFIED EXPERT
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.

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
Thanks, this was just the information I needed to solve it.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.