Microsoft KB Archive/316752

= How to programmatically change the database in a DSN that is using a trusted connection in Access 2002 =

Article ID: 316752

Article Last Modified on 8/11/2004

-

APPLIES TO


 * Microsoft Access 2002 Standard Edition

-



This article was previously published under Q316752



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

This article applies only to a Microsoft Access database (.mdb).

IN THIS TASK
SUMMARY
 * Steps to Programmatically Change a DSN

REFERENCES



SUMMARY
This step-by-step article shows you how to programmatically change a DSN that is using a trusted connection. You may want to use this technique because, if you use a DSN with a trusted connection to connect to SQL server, you cannot manually change the database when you are linking or importing tables. You may also want to use this technique if you do not want to create a separate DSN whenever you want to change the database. Finally, you may want to use this technique if you do not want to use SQL security. SQL security does allow you to select another database when you link or import tables.

back to the top

Steps to Programmatically Change a DSN
 Start Microsoft Access 2002, and then open any database. Create a new table that has two fields, one named Database and the other Description. Make the data type for both fields Text. Save the table as tblDatabases. Open the tblDatabases table, and then add data to the Databases and Descriptions fields. Create a new form. Add a combo box to the form with the following properties:

 Add a command button to the form. Change the Caption property of the command button to Change DSN.</li>  Set the OnClick property of the command button to the following event procedure. Make sure to change the values for the DataSourceName, DatabaseName, Description, DriverPath, LastUser, and Server variables as appropriate for your environment. 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 strTrusted As String Dim lResult As Long Dim hKeyHandle As Long

'Specify the DSN parameters.

DataSourceName = &quot;CodeTestSQL&quot; DatabaseName = Me.cboDatabases Description = Me.cboDatabases.Column(1) 'Path to your sqlsrv32.dll DriverPath = &quot;<Path to>\sqlsrv32.dll&quot; 'Your network user name LastUser = &quot; &quot; 'Your SQL server Server = &quot; &quot; DriverName = &quot;SQL Server&quot; strTrusted = &quot;yes&quot; 'Create the new DSN key.

lResult = RegCreateKey(HKEY_LOCAL_MACHINE, &quot;SOFTWARE\ODBC\ODBC.INI\&quot; & _          DataSourceName, hKeyHandle)

'Set the values of the new DSN key.

lResult = RegSetValueEx(hKeyHandle, &quot;Database&quot;, 0&, REG_SZ, _        ByVal DatabaseName, Len(DatabaseName)) lResult = RegSetValueEx(hKeyHandle, &quot;Description&quot;, 0&, REG_SZ, _        ByVal Description, Len(Description)) lResult = RegSetValueEx(hKeyHandle, &quot;Driver&quot;, 0&, REG_SZ, _        ByVal DriverPath, Len(DriverPath)) lResult = RegSetValueEx(hKeyHandle, &quot;LastUser&quot;, 0&, REG_SZ, _        ByVal LastUser, Len(LastUser)) lResult = RegSetValueEx(hKeyHandle, &quot;Server&quot;, 0&, REG_SZ, _        ByVal Server, Len(Server)) lResult = RegSetValueEx(hKeyHandle, &quot;Trusted_Connection&quot;, 0&, REG_SZ, _        ByVal strTrusted, Len(strTrusted)) '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, _        &quot;SOFTWARE\ODBC\ODBC.INI\ODBC Data Sources&quot;, hKeyHandle) lResult = RegSetValueEx(hKeyHandle, DataSourceName, 0&, REG_SZ, _        ByVal DriverName, Len(DriverName)) lResult = RegCloseKey(hKeyHandle) MsgBox &quot;Database changed to &quot; & Me.cboDatabases & &quot; in DSN CodeTestSQL!&quot; </li>  Type or paste the following in the General Declarations section of the code for the form: Private Const REG_SZ = 1   'Constant for a string variable type. Private Const HKEY_LOCAL_MACHINE = &H80000002

Private Declare Function RegCreateKey Lib &quot;advapi32.dll&quot; Alias _ &quot;RegCreateKeyA&quot; (ByVal hKey As Long, ByVal lpSubKey As String, _  phkResult As Long) As Long

Private Declare Function RegSetValueEx Lib &quot;advapi32.dll&quot; Alias _ &quot;RegSetValueExA&quot; (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 &quot;advapi32.dll&quot; _ (ByVal hKey As Long) As Long </li> Save the form as frmChangeDSN.</li> Open the frmChangeDSN form in Form view.</li> Select a database name from the combo box, and then click Change DSN.</li> Note that you receive a message box that confirms the database and DSN names. Click OK.</li> On the File menu, point to Get External Data, and then click Link.</li> Change the Files of Type box to ODBC Databases.</li> On the Machine Data Source tab, click CodeTestSQL, and then click OK.</li> Note that the tables that are listed are from the database that you selected on the form.</li> Click Cancel.</li> Return to the frmChangeDSN form, select a different database, and then click the command button.</li> Repeat steps 12 through 17. Note that the tables listed are from the new database that you selected.</li></ol>

back to the top

<div class="references_section">