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
ODBC User DSN for Access Using VB
Author:
Mkpandey
E-mail:
Click to e-mail author
Submitted:
7/14/2003
Version:
VB6
Compatibility:
VB6
Category:
Databases
Views:
20054
Shows how to manually create a user DSN. This code will help you to create your own ODBC User DSN to be used in modules for your project. This will be helpfull in preventing user from knowing your DB password and it will automatically create the DSN for Crystal reports etc.
Declarations:
'''''''''''''''''''''''''''''''''''''''''''''''''' 'Purpose : To create USER DSN through VB code 'By : Manish Kumar Pandey '''''''''''''''''''''''''''''''''''''''''''''''''' 'Put Following declaration in the form you want to create the dsn from..... '''''''''''''''''''''''''''''''''''''''''''''''''' 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:
'''''''''''''''''''''''''''''''''''''''''''''''''' ' And Now You need is just add commnd button and compy following code to its click event. '''''''''''''''''''''''''''''''''''''''''''''''''' 'Don forget to customize the varialbles according to your project..... '''' '''''''''''''''''''''''''''''''''''''''''''''''''' 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