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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

HOWTO: Programmatically Create a DSN for Client Access ODBC Driver 32-

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
Hello,

HOWTO: Programmatically Create a DSN for Client Access ODBC Driver 32-Bit with VB

I have tried to create the DSN for SQL Server, the code in VB is given below
how do i create the dsn for Client Access ODBC Driver 32-Bit with VB


--------------------------------------------------------------------------------

Option Explicit

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

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

Private 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

Private Declare Function RegCloseKey Lib "advapi32.dll" _
(ByVal hKey As Long) As Long



Private Sub Command1_Click()
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 lResult As Long
Dim hKeyHandle As Long

'Specify the DSN parameters.

DataSourceName = "MunSQL"
DatabaseName = "PUBS"
Description = "manually created DSN MUn"
DriverPath = "SQL Server"
LastUser = "sa"
Server = "MYSQLSERVER"
DriverName = "SQL Server"

'Create the new DSN key.

lResult = RegCreateKey(HKEY_LOCAL_MACHINE, "SOFTWARE\ODBC\ODBC.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, _
"SOFTWARE\ODBC\ODBC.INI\ODBC Data Sources", hKeyHandle)
lResult = RegSetValueEx(hKeyHandle, DataSourceName, 0&, REG_SZ, _
ByVal DriverName, Len(DriverName))
lResult = RegCloseKey(hKeyHandle)


End Sub

--------------------------------------------------------------------------------


Thanks

Regards,
Munawar Bootwala
------------------------------------------------------------
Software Engineer
Intentia South Asia (India) Pvt. Ltd.
 
For future reference, if you need VB help, post in a VB forum.

Here are a few other ways to create your DSN:
faq222-273

But if you insist on using the registry hack, create a new Access DSN named Temp. Open Regedit. Drill down to the following key:
HKEY_LOCAL_MACHINE\SOFTWARE\ODBC\ODBC.INI\Temp

There you'll find the keys/subkeys/values you'll need to replicate.

Also the name of the DSN for ODBC Manager at:
HKEY_LOCAL_MACHINE\SOFTWARE\ODBC\ODBC.INI\ODBC Data Sources Jon Hawkins
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top