ACC2000: How to Use SQLConfigDataSource to Create an Access System DSN

ID: Q231156


The information in this article applies to:

Advanced: Requires expert coding, interoperability, and multiuser skills.

This article applies to a Microsoft Access database (.mdb) and a Microsoft Access project (.adp).


SUMMARY

You cannot create a system DSN by using the RegisterDatabase method. To create a system DSN, use the ODBC API call for SQLConfigDataSource.

Microsoft provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support professionals can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific needs. If you have limited programming experience, you may want to contact a Microsoft Certified Solution Provider or the Microsoft fee-based consulting line at (800) 936-5200. For more information about Microsoft Certified Solution Providers, please see the following page on the World Wide Web:

http://www.microsoft.com/mcsp/
For more information about the support options available from Microsoft, please see the following page on the World Wide Web:

http://www.microsoft.com/support/supportnet/overview/overview.asp


MORE INFORMATION

The following example uses the API call for SQLConfigDataSource to create a system DSN. The example creates a data source for the sample database Northwind.mdb when the database is located at C:\Northwind.mdb.

  1. Copy the sample database Northwind.mdb to the root directory of drive C.


  2. Create a new Access database.


  3. Create a module and type the following lines in the Declarations section:


  4. 
    Option Explicit
    Const ODBC_ADD_SYS_DSN = 4       'Add data source
    Const ODBC_CONFIG_SYS_DSN = 5    'Configure (edit) data source
    Const ODBC_REMOVE_SYS_DSN = 6    'Remove data source
    
    Private Declare Function SQLConfigDataSource Lib "ODBCCP32.DLL" (ByVal _
       hwndParent As Long, ByVal fRequest As Long, ByVal _
       lpszDriver As String, ByVal lpszAttributes As String) As Long 
  5. Type the following procedure:


  6. 
    Function Build_SystemDSN(DSN_NAME As String, Db_Path As String)
    
       Dim ret%, Driver$, Attributes$
    
       Driver = "Microsoft Access Driver (*.MDB)" & Chr(0)
       Attributes = "DSN=" & DSN_NAME & Chr(0)
       Attributes = Attributes & "Uid=Admin" & Chr(0) & "pwd=" & Chr(0)
       Attributes = Attributes & "DBQ=" & Db_Path & Chr(0)
       
       ret = SQLConfigDataSource(0, ODBC_ADD_SYS_DSN, Driver, Attributes)
       
       'ret is equal to 1 on success and 0 if there is an error
       If ret <> 1 Then
           MsgBox "DSN Creation Failed"
       End If
    
    End Function 
  7. In the Immediate window, type the following line, and then press ENTER:


  8. 
    ? Build_SystemDSN("My SampleDSN","c:\Northwind.mdb") 
  9. Click Start, point to Settings, and then click Control Panel.


  10. In Control Panel, click ODBC Data Sources, ODBC Data Sources (32-bit), or 32bit ODBC.


  11. Click the System DSN tab. Note that My SampleDSN has been added to the System Data Sources list.


Additional query words: inf


Keywords          : kbdta ObcHowto 
Version           : WINDOWS:2000
Platform          : WINDOWS 
Issue type        : kbhowto 

Last Reviewed: July 23, 1999