473,382 Members | 1,348 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

How to Programatically make DSN to a Password protected Access DB

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

Please tell me how can I make DSN programatically to a password protected Access DB.


Thanks

Best Regards,

Iqbal
Jun 4 '07 #1
2 1917
sashi
1,754 Expert 1GB
Hi there,

Kindly refer to below code segment in-order to check / create DSN connection, hope it helps. Good luck & Take care.

Expand|Select|Wrap|Line Numbers
  1. 'in module file
  2. Private Const KEY_QUERY_VALUE = &H1
  3. Private Const ERROR_SUCCESS = 0&
  4. Private Const REG_SZ = 1
  5. Private Const HKEY_LOCAL_MACHINE = &H80000002
  6. Private Const REG_DWORD = 4
  7.  
  8.  
  9. Private 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
  10.  
  11.  
  12. Private Declare Function RegQueryValueEx Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, lpData As Any, lpcbData As Long) As Long ' Note that If you declare the lpData parameter as String, you must pass it By Value.
  13.  
  14.  
  15. Private Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
  16.  
  17.  
  18. Private Declare Function RegSetValueExString Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, ByVal lpValue As String, ByVal cbData As Long) As Long
  19.  
  20.  
  21. Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
  22.  
  23.  
  24. Private Declare Function RegSetValueExLong Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, lpValue As Long, ByVal cbData As Long) As Long
  25.  
  26.  
  27. Public Function isSZKeyExist(szKeyPath As String, _
  28.     szKeyName As String, _
  29.     ByRef szKeyValue As String) As Boolean
  30.     Dim bRes As Boolean
  31.     Dim lRes As Long
  32.     Dim hKey As Long
  33.     lRes = RegOpenKeyEx(HKEY_LOCAL_MACHINE, _
  34.     szKeyPath, _
  35.     0&, _
  36.     KEY_QUERY_VALUE, _
  37.     hKey)
  38.  
  39.  
  40.     If lRes <> ERROR_SUCCESS Then
  41.         isSZKeyExist = False
  42.         Exit Function
  43.     End If
  44.     lRes = RegQueryValueEx(hKey, _
  45.     szKeyName, _
  46.     0&, _
  47.     REG_SZ, _
  48.     ByVal szKeyValue, _
  49.     Len(szKeyValue))
  50.     RegCloseKey (hKey)
  51.  
  52.  
  53.     If lRes <> ERROR_SUCCESS Then
  54.         isSZKeyExist = False
  55.         Exit Function
  56.     End If
  57.     isSZKeyExist = True
  58. End Function
  59.  
  60.  
  61. Public Function checkAccessDriver(ByRef szDriverName As String) As Boolean
  62.     Dim szKeyPath As String
  63.     Dim szKeyName As String
  64.     Dim szKeyValue As String
  65.     Dim bRes As Boolean
  66.  
  67.  
  68.     bRes = False
  69.  
  70.     szKeyPath = "SOFTWARE\ODBC\ODBCINST.INI\Microsoft Access Driver (*.mdb)"
  71.     szKeyName = "Driver"
  72.     szKeyValue = String(255, Chr(32))
  73.  
  74.  
  75.  
  76.     If isSZKeyExist(szKeyPath, szKeyName, szKeyValue) Then
  77.         szDriverName = szKeyValue
  78.         bRes = True
  79.     Else
  80.         bRes = False
  81.     End If
  82.  
  83.     checkAccessDriver = bRes
  84. End Function
  85.  
  86.  
  87. Public Function checkWantedAccessDSN(szWantedDSN As String) As Boolean
  88.     Dim szKeyPath As String
  89.     Dim szKeyName As String
  90.     Dim szKeyValue As String
  91.     Dim bRes As Boolean
  92.  
  93.     szKeyPath = "SOFTWARE\ODBC\ODBC.INI\ODBC Data Sources"
  94.     szKeyName = szWantedDSN
  95.     szKeyValue = String(255, Chr(32))
  96.  
  97.  
  98.  
  99.     If isSZKeyExist(szKeyPath, szKeyName, szKeyValue) Then
  100.         bRes = True
  101.     Else
  102.         bRes = False
  103.     End If
  104.  
  105.     checkWantedAccessDSN = bRes
  106.  
  107. End Function
  108.  
  109.  
  110. Public Function createAccessDSN(szDriverName As String, _
  111.     szWantedDSN As String) As Boolean
  112.  
  113.     Dim hKey As Long
  114.     Dim szKeyPath As String
  115.     Dim szKeyName As String
  116.     Dim szKeyValue As String
  117.     Dim lKeyValue As Long
  118.     Dim lRes As Long
  119.     Dim lSize As Long
  120.     Dim szEmpty As String
  121.  
  122.     szEmpty = Chr(0)
  123.  
  124.  
  125.     lSize = 4
  126.     lRes = RegCreateKey(HKEY_LOCAL_MACHINE, _
  127.     "SOFTWARE\ODBC\ODBC.INI\" & _
  128.     szWantedDSN, _
  129.     hKey)
  130.  
  131.  
  132.  
  133.     If lRes <> ERROR_SUCCESS Then
  134.         createAccessDSN = False
  135.         Exit Function
  136.     End If
  137.  
  138.     lRes = RegSetValueExString(hKey, "UID", 0&, REG_SZ, _
  139.     szEmpty, Len(szEmpty))
  140.  
  141.     szKeyValue = App.Path & "\DB\ssmdb.mdb"
  142.     lRes = RegSetValueExString(hKey, "DBQ", 0&, REG_SZ, _
  143.     szKeyValue, Len(szKeyValue))
  144.     szKeyValue = szDriverName
  145.     lRes = RegSetValueExString(hKey, "Driver", 0&, REG_SZ, _
  146.     szKeyValue, Len(szKeyValue))
  147.     szKeyValue = "MS Access;"
  148.     lRes = RegSetValueExString(hKey, "FIL", 0&, REG_SZ, _
  149.     szKeyValue, Len(szKeyValue))
  150.     lKeyValue = 25
  151.     lRes = RegSetValueExLong(hKey, "DriverId", 0&, REG_DWORD, _
  152.     lKeyValue, 4)
  153.  
  154.     lKeyValue = 0
  155.     lRes = RegSetValueExLong(hKey, "SafeTransactions", 0&, REG_DWORD, _
  156.     lKeyValue, 4)
  157.  
  158.     lRes = RegCloseKey(hKey)
  159.     szKeyPath = "SOFTWARE\ODBC\ODBC.INI\" & szWantedDSN & "\Engines\Jet"
  160.  
  161.     lRes = RegCreateKey(HKEY_LOCAL_MACHINE, _
  162.     szKeyPath, _
  163.     hKey)
  164.  
  165.  
  166.  
  167.     If lRes <> ERROR_SUCCESS Then
  168.         createAccessDSN = False
  169.         Exit Function
  170.     End If
  171.     lRes = RegSetValueExString(hKey, "ImplicitCommitSync", 0&, REG_SZ, _
  172.     szEmpty, Len(szEmpty))
  173.     szKeyValue = "Yes"
  174.     lRes = RegSetValueExString(hKey, "UserCommitSync", 0&, REG_SZ, _
  175.     szKeyValue, Len(szKeyValue))
  176.     lKeyValue = 2048
  177.     lRes = RegSetValueExLong(hKey, "MaxBufferSize", 0&, REG_DWORD, _
  178.     lKeyValue, 4)
  179.  
  180.     lKeyValue = 5
  181.     lRes = RegSetValueExLong(hKey, "PageTimeout", 0&, REG_DWORD, _
  182.     lKeyValue, 4)
  183.  
  184.     lKeyValue = 3
  185.     lRes = RegSetValueExLong(hKey, "Threads", 0&, REG_DWORD, _
  186.     lKeyValue, 4)
  187.  
  188.     lRes = RegCloseKey(hKey)
  189.     lRes = RegCreateKey(HKEY_LOCAL_MACHINE, _
  190.     "SOFTWARE\ODBC\ODBC.INI\ODBC Data Sources", _
  191.     hKey)
  192.  
  193.  
  194.  
  195.     If lRes <> ERROR_SUCCESS Then
  196.         createAccessDSN = False
  197.         Exit Function
  198.     End If
  199.  
  200.     szKeyValue = "Microsoft Access Driver (*.mdb)"
  201.     lRes = RegSetValueExString(hKey, szWantedDSN, 0&, REG_SZ, _
  202.     szKeyValue, Len(szKeyValue))
  203.  
  204.     lRes = RegCloseKey(hKey)
  205.     createAccessDSN = True
  206. End Function
  207. 'anywhere in application
  208.  
  209. Dim szDriverName As String
  210. Dim szWantedDSN As String
  211.  
  212. szDriverName = String(255, Chr(32))
  213. szWantedDSN = "MyAccess_ODBC"
  214. 'is access drivers installed?
  215.  
  216.  
  217. If Not checkAccessDriver(szDriverName) Then
  218.     MsgBox "You must Install Access ODBC Drivers before use this program.", vbOK + vbCritical
  219. End If
  220.  
  221. 'is our dsn exist?
  222.  
  223.  
  224. If Not (checkWantedAccessDSN(szWantedDSN)) Then
  225.  
  226.  
  227.     If szDriverName = "" Then
  228.         MsgBox "Can't find access ODBC driver.", vbOK + vbCritical
  229.     Else
  230.  
  231.  
  232.         If Not createAccessDSN(szDriverName, szWantedDSN) Then
  233.             MsgBox "Can't create database ODBC.", vbOK + vbCritical
  234.         End If
  235.     End If
  236. End If
  237.  
--------------------------------------------------------------------------------

Please tell me how can I make DSN programatically to a password protected Access DB.


Thanks

Best Regards,

Iqbal
Jun 4 '07 #2
MMcCarthy
14,534 Expert Mod 8TB
Hi there,

Kindly refer to below code segment in-order to check / create DSN connection, hope it helps. Good luck & Take care.
Hi, welcome back.

Mary
Jun 9 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Kelly Bowles | last post by:
I have made a purchase requistion which I have been asked to put password protected copy of requestors signature. I am thinking that each signature will have to be scanned as a picture and linked...
3
by: MissiMaths | last post by:
I have used outputTo to export a copy of a table out of access into excel. I was wondering if there is anyway to set a password on this within the code so that it is read only in .xls format? I...
4
by: Kevin Vogler | last post by:
I need to read the value of a hidden field on a remote HTML page and then use that value in a form submit on my local page. How can I read the hidden field programatically? Thanks in advance....
4
by: kthiagar | last post by:
Hi I am trying to connect to a password protected access file from VB.NET. I have no problem in connecting to Access, if I remove the password. This is what I am doing: In the server explorer,...
3
by: Miro | last post by:
Why Password protect an MDB when someone can google and get a hack? Wondering if anyone else has thought of this and just said "oh well"... I plan to password protect an MDB where I have some...
3
by: Noel S Pamfree | last post by:
Problem 1 ======= I need to create a page for a friend who operates a school website. She needs to set up a page so that only the Governors can access it. I thought I'd try to use JavaScript to...
3
by: -xx- | last post by:
Hi, I am new to c# and just trying out c# with ms access. First problem i encountered is how do i open up ms access that has password. Here is my current codes which working fine if there is...
8
by: gngui | last post by:
Hi, Please Please Please anyone out there, if you have a solution, i will highly appreciate as i have searched the internet for 5 days now with no hope. 1st, the versions i am using are Ms Access...
4
n8kindt
by: n8kindt | last post by:
i'm trying to create a secure scenario where no one will be able to know our encrypted database passcode. BUT if they login to a form in another database successfully, it will open the encrypted...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.