Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Automatic Creation of System DSN - please help. 1

Status
Not open for further replies.

ChrisSommer

Programmer
Oct 17, 1999
2
US
Is there any way to automatically create a System DSN during the installation of a program? I would like my users NOT to have to go into the control panel and set it up themselves or go around to each machine and do it myself if possible.<br>
<br>
Thanks in advance.
 
Option Explicit<br>
<br>
Private Const REG_SZ = 1 'Constant for a string variable type.<br>
Private Const HKEY_LOCAL_MACHINE = &H80000002<br>
<br>
Private Declare Function RegCreateKey Lib &quot;advapi32.dll&quot; Alias _<br>
&quot;RegCreateKeyA&quot; (ByVal hKey As Long, ByVal lpSubKey As String, _<br>
phkResult As Long) As Long<br>
<br>
Private Declare Function RegSetValueEx Lib &quot;advapi32.dll&quot; Alias _<br>
&quot;RegSetValueExA&quot; (ByVal hKey As Long, ByVal lpValueName As String, _<br>
ByVal Reserved As Long, ByVal dwType As Long, lpData As Any, ByVal _<br>
cbData As Long) As Long<br>
<br>
Private Declare Function RegCloseKey Lib &quot;advapi32.dll&quot; _<br>
(ByVal hKey As Long) As Long <br>
<br>
<br>
Private Sub Command1_Click()<br>
<br>
Dim DataSourceName As String<br>
Dim DatabaseName As String<br>
Dim Description As String<br>
Dim DriverPath As String<br>
Dim DriverName As String<br>
Dim LastUser As String<br>
Dim Regional As String<br>
Dim Server As String<br>
<br>
Dim lResult As Long<br>
Dim hKeyHandle As Long<br>
<br>
'Specify the DSN parameters.<br>
<br>
DataSourceName = &quot;&lt;the name of your new DSN&gt;&quot;<br>
DatabaseName = &quot;&lt;name of the database to be accessed by the new DSN&gt;&quot;<br>
Description = &quot;&lt;a description of the new DSN&gt;&quot;<br>
DriverPath = &quot;&lt;path to your SQL Server driver&gt;&quot;<br>
LastUser = &quot;&lt;default user ID of the new DSN&gt;&quot;<br>
Server = &quot;&lt;name of the server to be accessed by the new DSN&gt;&quot;<br>
DriverName = &quot;SQL Server&quot;<br>
<br>
'Create the new DSN key.<br>
<br>
lResult = RegCreateKey(HKEY_LOCAL_MACHINE, &quot;SOFTWARE\ODBC\ODBC.INI\&quot; & _<br>
DataSourceName, hKeyHandle)<br>
<br>
'Set the values of the new DSN key.<br>
<br>
lResult = RegSetValueEx(hKeyHandle, &quot;Database&quot;, 0&, REG_SZ, _<br>
ByVal DatabaseName, Len(DatabaseName))<br>
lResult = RegSetValueEx(hKeyHandle, &quot;Description&quot;, 0&, REG_SZ, _<br>
ByVal Description, Len(Description))<br>
lResult = RegSetValueEx(hKeyHandle, &quot;Driver&quot;, 0&, REG_SZ, _<br>
ByVal DriverPath, Len(DriverPath))<br>
lResult = RegSetValueEx(hKeyHandle, &quot;LastUser&quot;, 0&, REG_SZ, _<br>
ByVal LastUser, Len(LastUser))<br>
lResult = RegSetValueEx(hKeyHandle, &quot;Server&quot;, 0&, REG_SZ, _<br>
ByVal Server, Len(Server))<br>
<br>
'Close the new DSN key.<br>
<br>
lResult = RegCloseKey(hKeyHandle)<br>
<br>
'Open ODBC Data Sources key to list the new DSN in the ODBC Manager.<br>
'Specify the new value.<br>
'Close the key.<br>
<br>
lResult = RegCreateKey(HKEY_LOCAL_MACHINE, _<br>
&quot;SOFTWARE\ODBC\ODBC.INI\ODBC Data Sources&quot;, hKeyHandle)<br>
lResult = RegSetValueEx(hKeyHandle, DataSourceName, 0&, REG_SZ, _<br>
ByVal DriverName, Len(DriverName))<br>
lResult = RegCloseKey(hKeyHandle)<br>
<br>
End Sub <br>
<br>
<br>
<p>Eric De Decker<br><a href=mailto:vbg.be@vbgroup.nl>vbg.be@vbgroup.nl</a><br><a href= Basic Center</a><br>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top