Tip

ODBC connection to SQL server using LotusScript

You can create an ODBC connection to a SQL server using LotusScript with the click of a button. I put this button in my About Database document.

VIEW MEMBER FEEDACK TO THIS TIP
  
Code: Option Explicit


Private Const REG_SZ = 1    
'Constant for a string variable type.
Private Const HKEY_LOCAL_MACHINE = 
&H80000002

Declare Function RegCreateKey Lib "advapi32.dll"
 Alias "RegCreateKeyA" (Byval 
hKey As Long, Byval lpSubKey As String, 

phkResult As Long) As Long

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

Declare Function RegCloseKey Lib "advapi32.dll" 
(Byval hKey As Long) As Long

Sub Click(Source As 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 Password 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 = ""     'Optional <path to your 
SQL Server driver>
 LastUser = "<default user ID of the new DSN>"    
 Password = "<Password>"
 Server = "'<name of the server to be 
accessed by the new DSN>"    
 DriverName = "SQL Server"
 
   'Create the new DSN key.
 
 lResult = RegCreateKey(HKEY_LOCAL_MACHINE,
 "SOFTWAREODBCODBC.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, _
 "SOFTWAREODBCODBC.INIODBC Data Sources", 
hKeyHandle)
 lResult = RegSetValueEx(hKeyHandle,
DataSourceName, 0&, REG_SZ, _
 Byval DriverName, Len(DriverName))
 lResult = RegCloseKey(hKeyHandle)
 
End Sub

MEMBER FEEDBACK TO THIS TIP

The author seems to have lost some backslashes in the RegCreateKey function. The following lines:

lResult = RegCreateKey(HKEY_
LOCAL_MACHINE,
 "SOFTWAREODBCODBC.INI" & _
 DataSourceName, hKeyHandle)
should be:
 lResult = RegCreateKey
(HKEY_LOCAL_MACHINE,
 "SOFTWARE\
ODBC\ODBC.INI" & _
 DataSourceName, hKeyHandle)
 

And also, the following lines:

lResult = RegCreateKey(HKEY_
LOCAL_MACHINE, _
 "SOFTWAREODBCODBC.INIODBC 
Data Sources", 
hKeyHandle)
 
should be:
lResult = RegCreateKey
(HKEY_LOCAL_MACHINE, _
 "SOFTWARE\ODBC
\ODBC.INI\ODBC 
Data Sources", 
hKeyHandle)

—Hans F.

Do you have comments on this tip? Let us know.

This tip was submitted to the SearchDomino.com tip exchange by member Tina Butler. Please let others know how useful it is via the rating scale below. Do you have a useful Notes/Domino tip or code to share? Submit it to our monthly tip contest and you could win a prize and a spot in our Hall of Fame.

This was first published in March 2005

There are Comments. Add yours.

 
TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.