ODBC connection to SQL server using LotusScript
This tip shows you how to create an ODBC connection to a SQL server using LotusScript with the click of a button.
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.
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
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.