User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Visual Basic 4 / 5 / 6 section within the Software Development category of DaniWeb, a massive community of 397,698 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,521 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Visual Basic 4 / 5 / 6 advertiser:
Jul 12th, 2005
Views: 28,473
This is the coding needed to password any button on any switchboard from used in Access Database
visualbasic Syntax | 4 stars
  1. 'NOTE: The sample code in this article uses Microsoft Data
  2. 'Access Objects. For this code to run properly, you must reference
  3. 'the Microsoft DAO 3.6 Object Library. To do so, click References
  4. 'on the Tools menu in the Visual Basic Editor, and make sure
  5. 'that the Microsoft DAO 3.6 Object Library check box is selected.
  6.  
  7.  
  8. 'Using Code to Password Protect a Form
  9. 'By using code, you can prompt for a password when a user
  10. 'opens a form or a report. If the correct password is entered,
  11. 'the form or the report is opened.
  12.  
  13. 'The following example shows you how you can password protect
  14. 'the Orders form in the sample database Northwind.mdb:
  15. '1. Start Access and then open the sample
  16. 'database Northwind.mdb.
  17. '2. Press ALT+F11 to start the Microsoft Visual Basic editor.
  18. '3. On the Insert menu, click Module.
  19. '4. In the module sheet, type the following procedure:
  20.  
  21. Public MyPassword Public Function KeyCode(Password As String) As Long
  22. ' This function will produce a unique key for the ' string that is passed
  23. 'in as the Password.
  24.  
  25. Dim I As Integer
  26. Dim Hold As Long
  27.  
  28. For I = 1To Len(Password)
  29. Select Case (Asc(Left(Password, 1)) * I) Mod 4
  30. Case Is = 0
  31. Hold = Hold + (Asc(Mid(Password, I, 1)) * I)
  32. Case Is = 1
  33. Hold = Hold - (Asc(Mid(Password, I, 1)) * I)
  34. Case Is = 2
  35. Hold = Hold + (Asc(Mid(Password, I, 1)) * _ (I - Asc(Mid(Password, I, 1))))
  36. Case Is = 3
  37. Hold = Hold - (Asc(Mid(Password, I, 1)) * _ (I + Len(Password)))
  38. End Select
  39. Next I
  40. KeyCode = Hold
  41. End Function
  42.  
  43.  
  44.  
  45.  
  46. '5. Press ALT+F11 to return to Access.
  47. '6. In the Database window, under Objects, click Tables,
  48. 'and then click New.
  49. '7. In the New Table dialog box, double-click Design View.
  50. '8. Create a new table as follows:
  51.  
  52. 'Table: tblPassword
  53. 'Field Name: ObjectName Data Type: Text Field Size: 50
  54. 'Field Name: KeyCode Data Type: Text Field Size:
  55. '25 Input Mask: Password Table Properties: tblPassword
  56. 'PrimaryKey: ObjectName
  57.  
  58. '9. Open the tblPassword table and then enter the following data:
  59.  
  60. 'ObjectName: Orders (ObjectName is the name of the form you
  61. 'want to go to)
  62. 'KeyCode: 2818
  63.  
  64. '10. Create a new form in design view and save the form
  65. 'as frmPassword.
  66. '11. Add a single textbox to frmPassword called Text0,
  67. 'and a command button called CheckPassword.
  68. '12. Set the Input Mask property of Text0 to "PASSWORD"
  69. '(minus the quotation marks).
  70. '13. Add the following code to the OnClick Event of
  71. 'the CheckPassword button and then save the form:
  72.  
  73.  
  74. If IsNull(Forms!frmPassword!Text0.Value) Then
  75. MsgBox "You cannot enter a blank Password. Try again."
  76. Me!Text0.SetFocus
  77. Else
  78. MyPassword = Me!Text0.Value
  79. DoCmd.Close acForm, "frmPassword"
  80. End If
  81.  
  82.  
  83.  
  84.  
  85. '14. Open Switchboard Table and Print it off You will need
  86. 'this information
  87. '15. Open the Main Switchboard form in Design view.
  88. 'Then open the VB Editor.
  89. '16. Apply the following code where indicated in the code.
  90. 'The thing you need to remember is the SwitchboardID
  91. 'is 0 for default and the ItemNumber is 2 for the 3rd button
  92. 'on the main switchboard. By changing these in the
  93. 'IF statement you can direct which button to have
  94. 'the Password on.
  95.  
  96.  
  97. Select Case rs![Command]
  98.  
  99. ' Go to another switchboard.
  100. Case conCmdGotoSwitchboard
  101. Me.Filter = "[ItemNumber] = 0 AND [SwitchboardID]=" & rs![Argument]
  102.  
  103.  
  104. 'Below is the modified code I put into the Switch board
  105. Dim Hold As Variant
  106. Dim tmpKey As Long
  107. Dim I As Integer
  108. Dim rs1 As DAO.Recordset
  109. Dim db As DAO.Database
  110.  
  111. Me.Filter = "[ItemNumber] = 0 AND [SwitchboardID]=" & rs![Argument]
  112.  
  113. ‘ Below is where you set the button
  114.  
  115. If Val([ItemNumber]) = 0 And Val([SwitchboardID]) = 2 Then
  116. DoCmd.OpenForm "frmPassword", acNormal, , , , acDialog
  117. Hold = MyPassword
  118. Set db = CurrentDb
  119. Set rs1 = db.OpenRecordset("tblPassword", dbOpenTable)
  120. If rs1.NoMatch Then
  121. MsgBox "Sorry cannot find password information. Try Again"
  122. Cancel = -1
  123.  
  124. ' Move to the switchboard page that is marked as the default.
  125.  
  126. Me.Filter = "[ItemNumber] = 0 AND [Argument] = 'Default' "
  127. Me.FilterOn = True
  128.  
  129. ElseIf Not (rs1![KeyCode] = KeyCode(CStr(Hold))) Then
  130. MsgBox "Sorry password does not match Key Code." & _
  131. "Try again.", vbOKOnly, "Incorrect Password"
  132. Cancel = -1
  133.  
  134. ' Move to the switchboard page that is marked as the default.
  135.  
  136. Me.Filter = "[ItemNumber] = 0 AND [Argument] = 'Default' "
  137. Me.FilterOn = True
  138. End If
  139. rs1.Close
  140. db.Close
  141. End If
  142.  
  143.  
  144. '17. Close and then save the Switchboard.
  145. '18. Open the Switchboard and select your button it shoud
  146. 'open the frmPassword
  147.  
  148. 'Notice that the Orders form opens. The KeyCode that is generated
  149. 'by PASSWORD matches the KeyCode in the tblPassword table,
  150. 'and is dependent on the case of the letters in the password entered.
  151.  
  152. '19. Close and then reopen the Orders form and then
  153. 'type PassWord when you are prompted for a password.
  154.  
  155. 'Notice that you receive the message:
  156.  
  157. 'Sorry you entered the wrong password. Try again.
  158. 'The Switchboard does not open and is redirected to
  159. 'Default Switchboard because the password procedure is case-sensitive.
  160.  
  161. '20. To determine what the corresponding KeyCode is for
  162. 'a particular string, type the following in the Immediate
  163. 'window and then press ENTER:
  164.  
  165. '?KeyCode("TestString")
  166.  
  167. 'The earlier example returns 5864.
  168.  
  169. '21. To hide the tblPassword table in the Database window,
  170. 'right-click the tblPassword table, and then click Properties.
  171. 'In the Properties window, click to select the Hidden check
  172. 'box, and then click OK.
  173.  
  174.  
Comments (Newest First)
rose_an | Newbie Poster | Jun 20th, 2008
can we edit excel sheets inside vb?
tongs | Newbie Poster | Mar 15th, 2007
:rolleyes:
Drisconsult | Newbie Poster | Jun 13th, 2006
How can the password, "PASSWORD" enhance security? Surely the code should allow the use of any text for a password, or am I being dumb today?

Regards
Terence
jayp2002 | Newbie Poster | Nov 16th, 2005
yeaq good work.but am getting an eror on this peice of line what do i do
Public MyPassword Public Function KeyCode(Password As String) As Long
Post Comment

Only community members can submit or comment on code snippets. You must register or log in to contribute.

DaniWeb Marketplace (Sponsored Links)
All times are GMT -4. The time now is 1:24 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC