Tip

Setup An Odbc Connection Automatically

This script show you how to setup an ODBC connection without any user
intervention. The script uses the Windows Registry and works on Windows
95/98/NT
Declaration Event:
Public Const REG_SZ = 1 'Unicode nul terminated string
Public Const REG_DWORD = 4 '32-bit number
Public Const HKEY_CURRENT_USER = &H80000001
Public Const HKEY_LOCAL_MACHINE = &H80000002
Public Const SYNCHRONIZE = &H100000
Public Const STANDARD_RIGHTS_ALL = &H1F0000
Public Const STANDARD_RIGHTS_READ = &H20000
Public Const KEY_QUERY_VALUE = &H1
Public Const KEY_SET_VALUE = &H2
Public Const KEY_CREATE_SUB_KEY = &H4
Public Const KEY_ENUMERATE_SUB_KEYS = &H8
Public Const KEY_NOTIFY = &H10
Public Const KEY_CREATE_LINK = &H20
Public Const KEY_ALL_ACCESS = ((STANDARD_RIGHTS_ALL Or KEY_QUERY_VALUE Or
KEY_SET_VALUE Or KEY_CREATE_SUB_KEY Or KEY_ENUMERATE_SUB_KEYS Or KEY_NOTIFY Or
KEY_CREATE_LINK) And (Not SYNCHRONIZE))
Public Const KEY_READ = ((STANDARD_RIGHTS_READ Or KEY_QUERY_VALUE Or
KEY_ENUMERATE_SUB_KEYS Or KEY_NOTIFY) And (Not SYNCHRONIZE))
Public Const ERROR_SUCCESS = 0&

Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias "RegOpenKeyExA" (Byval
hKey As Long, Byval lpSubKey As String, Byval ulOptions As Long, Byval
samDesired As Long, phkResult As Long) As Long
Declare Function RegCloseKey Lib "advapi32.dll" (Byval hKey As Long) As Long
Declare Function RegQueryValueEx Lib "advapi32.dll" Alias "RegQueryValueExA"
(Byval hKey As Long, Byval lpValueName As String, Byval lpReserved As Long,
lpType As Long, Byval lpData As String, lpcbData 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, Byval lpData As String, Byval cbData As Long) As Long
Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" (Byval
hKey As Long, Byval lpSubKey As String, phkResult As Long) As Long
Declare Function RegEnumValue Lib "advapi32.dll" Alias "RegEnumValueA" (Byval
hKey As Long, Byval dwIndex As Long, Byval lpValueName As String, lpcbValueName
As Long, Byval lpReserved As Long, lpType As Long, lpData As Any, lpcbData As
Long) As Long



Sub Initialize()
Dim session As New NotesSession
Dim db As NotesDatabase
Set db = session.CurrentDatabase
Dim hKeyHandle As Long
Dim RetType As Long
Dim RetSize As Long
Dim lValueLen As Long
Dim lDataLen As Long
Dim lpReturnedString As String*127

RetSize = 126
lngCurIdx = 0
lResult = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
"SOFTWARE\ODBC\ODBCINST.INI\ODBC Drivers" , 0, KEY_READ, hKeyHandle)

'Check if an ODBC Driver exist on the user workstation
Do
lValueLen = 2000
strValue = String(lValueLen, 0)
lDataLen = 2000
lResult = RegEnumValue(hKeyHandle, lngCurIdx, Byval strValue,
lValueLen, 0&, REG_DWORD, Byval lngData, lDataLen)
lngCurIdx = lngCurIdx + 1

If Instr(1, strValue, "Lotus NotesSQL") Then
lResult = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
"SOFTWARE\ODBC\ODBCINST.INI\" & strValue, 0, KEY_READ, hKeyHandle)
lResult = RegQueryValueEx(hKeyHandle, "Driver", 0, RetType,
lpReturnedString, RetSize)

'Specified the User DSN parameters
DataSourceName = db.Title
DatabaseName = db.FilePath
DriverPath = lpReturnedString
KeepTempIdx = "0"
MapSpecialChars = "Y"
MaxLongVarcharLen = "512"
MaxRels = "20"
MaxStmtLen = "4096"
MaxSubquery = "20"
MaxVarcharLen = "254"
Server = db.Server
ShowImplicitFlds = "0"
ThreadTimeout = "60"
DriverName = strValue

'Add the new ODBC connection key
lResult = RegCreateKey(HKEY_CURRENT_USER,
"SOFTWARE\ODBC\ODBC.INI\" & DataSourceName, hKeyHandle)
lResult = RegSetValueEx(hKeyHandle, "Database", 0&, REG_SZ,
Byval DatabaseName, Len(DatabaseName))
lResult = RegSetValueEx(hKeyHandle, "Driver", 0&, REG_SZ, Byval &LTWHA

This was first published in November 2000

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.