Find Code:
All Words
Any of the Words
Exact Phrase
Home
:
Code
:
Forums
:
Submit
:
Mailing List
:
About
:
Contact
Code
All
VB.NET
ASP.NET
C#
VB Classic
ASP Classic
Snippets
Popular
Resources
Submit Code
Forums
Articles
Tips
Links
Books
Contest
Link to us
User Dsn For Access Through VB
Author:
Mkpandeymsc
E-mail:
Click to e-mail author
Submitted:
7/24/2003
Version:
VB6
Compatibility:
VB6
Category:
Windows API
Views:
22238
This can create new User DSN for MS Access Database through VB. This is usefull in Crystal Report etc. Take a form, put a Commond button on it and copy this code and declaration to the form's code window.
Declarations:
'''''''''''''''''''' '''User DSN Trough VB Code '''By M K Pandey ''''''''''''''''''''''' Option Explicit Private Const REG_DWORD = 4& Private Const REG_SZ = 1 'Constant for a string variable type. Private Const HKEY_CURRENT_USER = &H80000001 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 ''''''''''''''''''''''''''''''''''''''''''''''
Code:
Private Sub Command1_Click() Dim DataSourceName As String Dim DBQ As String Dim Description As String Dim DriverPath As String Dim DriverId As Long Dim DriverName As String Dim User As String Dim PWD As String Dim lResult As Long Dim hKeyHandle As Long Dim hKeyHandSub As Long 'Specify the DSN parameters. DataSourceName = "
" DBQ = "
" Description = "
" DriverPath = "C:\windows\System\odbcjt32.dll" PWD = "
" DriverId = 19 User = "admin" DriverName = "MIcrosoft Access Driver (*.mdb)" 'Create the new DSN key. lResult = RegCreateKey(HKEY_CURRENT_USER, "SOFTWARE\ODBC\ODBC.INI\" & _ DataSourceName, hKeyHandle) 'Set the values of the new DSN key. lResult = RegSetValueEx(hKeyHandle, "DBQ", 0&, REG_SZ, _ ByVal DBQ, Len(DBQ)) 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, "DriverID", 0&, REG_DWORD, _ 25, 4) lResult = RegSetValueEx(hKeyHandle, "FIL", 0&, REG_SZ, _ ByVal "MS Access", 9) lResult = RegSetValueEx(hKeyHandle, "PWD", 0&, REG_SZ, _ ByVal PWD, Len(PWD)) lResult = RegSetValueEx(hKeyHandle, "SafeTransactions", 0&, REG_DWORD, _ 0, 4) lResult = RegSetValueEx(hKeyHandle, "UID", 0&, REG_SZ, _ ByVal User, Len(User)) ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' 'Open a new key as follows lResult = RegCreateKey(HKEY_CURRENT_USER, "SOFTWARE\ODBC\ODBC.INI\" & _ DataSourceName & "\Engines\Jet", hKeyHandSub) ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' lResult = RegSetValueEx(hKeyHandSub, "ImplicitCommitSync", 0&, REG_SZ, _ ByVal "", 0) lResult = RegSetValueEx(hKeyHandSub, "MaxBufferSize", 0&, REG_DWORD, _ 2048, 4) lResult = RegSetValueEx(hKeyHandSub, "PageTimeout", 0&, REG_DWORD, _ 5, 4) lResult = RegSetValueEx(hKeyHandSub, "Threads", 0&, REG_DWORD, _ 3, 4) lResult = RegSetValueEx(hKeyHandSub, "UserCommitSync", 0&, REG_SZ, _ ByVal "Yes", 3) ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' 'Close the new Sub key. lResult = RegCloseKey(hKeyHandSub) ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' '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_CURRENT_USER, _ "SOFTWARE\ODBC\ODBC.INI\ODBC Data Sources", hKeyHandle) lResult = RegSetValueEx(hKeyHandle, DataSourceName, 0&, REG_SZ, _ ByVal DriverName, Len(DriverName)) lResult = RegCloseKey(hKeyHandle) End Sub
Home
|
Forums
|
Submit
|
Books
|
Mailing List
|
Advertising
|
About
|
Contact
© 2024 A1VBCode. All rights reserved.
Legal disclaimer & terms of use
Privacy statement