[Webinar] Streamline your web hosting managementRegister Today


Create SQL Server DSN

Posted on 1999-12-12
Medium Priority
Last Modified: 2013-12-25
I wanna create a SQL Server DSN programatically using WinAPI, anybody can give me sample coding?

Question by:slgal
  • 3
  • 3

Accepted Solution

deepakg earned 200 total points
ID: 2275960
The foll. code must be in the general declarations.

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

The foll. code can be in the Click() Event of a button ....

   Dim DataSourceName As String
   Dim DatabaseName As String
   Dim Description As String
   Dim DriverPath As String
   Dim DriverName As String
   Dim LastUser As String
   Dim Regional As String
   Dim Server As String

   Dim lResult As Long
   Dim hKeyHandle As Long

   'Specify the DSN parameters.

   DataSourceName = "<the name of your new DSN>"
   DatabaseName = "<name of the database to be accessed by the new DSN>"
   Description = "<a description of the new DSN>"
   DriverPath = "<path to your SQL Server driver>"
   LastUser = "<default user ID of the new DSN>"
   Server = "<name of the server to be accessed by the new DSN>"
   DriverName = "SQL Server"

   '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)

Hope this helps....


Author Comment

ID: 2276015
As I know, there is a API function called "SQLConfigDataSource", I have been sucessfully use it for Access database. Can I use it us SQL Server?


Expert Comment

ID: 2276026
Yes, You can use this API also like this

#If WIN32 Then
          Dim intRet As Long
          Dim intRet As Integer
      #End If
      Dim strDriver As String
      Dim strAttributes As String

      'Set the driver to SQL Server because it is most common.
      strDriver = "SQL Server"
      'Set the attributes delimited by null.
      'See driver documentation for a complete
      'list of supported attributes.
      strAttributes = "SERVER=SomeServer" & Chr$(0)
      strAttributes = strAttributes & "DESCRIPTION=Temp DSN" & Chr$(0)
      strAttributes = strAttributes & "DSN=DSN_TEMP" & Chr$(0)
      strAttributes = strAttributes & "DATABASE=pubs" & Chr$(0)
      strAttributes = strAttributes & "UID=sa" & Chr$(0)
      strAttributes = strAttributes & "PWD=" & Chr$(0)
      'To show dialog, use Form1.Hwnd instead of vbAPINull.
      intRet = SQLConfigDataSource(vbAPINull, ODBC_ADD_DSN, _
      strDriver, strAttributes)
      If intRet Then
          MsgBox "DSN Created"
          MsgBox "Create Failed"
      End If

Hope this helps...

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

LVL 71

Expert Comment

by:Éric Moreau
ID: 2276874
Take a look at the RegisterDatabase (of DAO) or rdoRegisterDatabase (of RDO). It registers perfectly.

Author Comment

ID: 2296706
Sorry for such a long delay. Can you tell me where I can find the documentation on this topic? I can find the function declaration in MSDN, but the constant declaration of the winapi is not available. Also, driver documentation is not available :<


Expert Comment

ID: 2296830
Hi slgal,
Long time indeed!!
As far as the documentation is concerned, check the MSDN with the foll:
HOWTO: Programmatically Create a DSN for SQL Server with VB
Constant declarations can be obtained from API Viewer or Win32api.txt file (search this file on the HDD)
Even I was not able to obtain driver specific documentation, which is something I need at times while connecting VB with Oracle/DB2, etc.
Get back anytime..

Author Comment

ID: 2299310
Thx, even I cannot find those ODBC constant declaration in Win32api, I think it's time to give you the points :>

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

612 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