Microsoft KB Archive/179371

= HOWTO: ACC: Create a Password Protected Form or Report =

Article ID: 179371

Article Last Modified on 1/22/2007

-

APPLIES TO


 * Microsoft Access 95 Standard Edition
 * Microsoft Access 97 Standard Edition

-



This article was previously published under Q179371



Advanced: Requires expert coding, interoperability, and multiuser skills.



SUMMARY
Microsoft Access has two built-in security features for protecting your database:
 * User/Group accounts and permissions
 * database passwords

This article shows you how you can also set individual passwords for each form and for each report in your database.



MORE INFORMATION
Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements. By using code, you can prompt for a password when a user opens a form or a report. If the correct password is entered, the form or the report is opened.

The following example shows you how you can password protect the Orders form in the sample database Northwind.mdb:  Open the sample database Northwind.mdb.  Create a module, and then type the following procedure: Public Function KeyCode(Password As String) As Long ' This function will produce a unique key for the ' string that is passed in as the Password. Dim I As Integer Dim Hold As Long

For I = 1 To Len(Password) Select Case (Asc(Left(Password, 1)) * I) Mod 4 Case Is = 0 Hold = Hold + (Asc(Mid(Password, I, 1)) * I)           Case Is = 1 Hold = Hold - (Asc(Mid(Password, I, 1)) * I)           Case Is = 2 Hold = Hold + (Asc(Mid(Password, I, 1)) * _                 (I - Asc(Mid(Password, I, 1)))) Case Is = 3 Hold = Hold - (Asc(Mid(Password, I, 1)) * _                 (I + Len(Password))) End Select Next I        KeyCode = Hold End Function   Create a new table as follows:      Table: tblPassword ---     Field Name: ObjectName Data Type: Text Field Size: 50 Field Name: KeyCode Data Type: Number Field Size: Long Integer Input Mask: Password

Table Properties: tblPassword -     PrimaryKey: ObjectName   Open the tblPassword table and enter the following data:      ObjectName: Orders KeyCode: 2818  Open the Orders form in Design view.</li>  Set the OnOpen property of the form to the following event procedure: Private Sub Form_Open(Cancel as Integer) Dim Hold As Variant Dim tmpKey As Long Dim I As Integer Dim rs As Recordset Dim db As Database

On Error GoTo Error_Handler ' Check to see if the user is passing in the Password. Hold = InputBox("Please Enter Your Password", "Enter Password") ' Open the table that contains the password. Set db = CurrentDb Set rs = db.OpenRecordset("tblPassword", dbOpenTable) rs.Index = "PrimaryKey" rs.Seek "=", Me.Name If rs.NoMatch Then MsgBox "Sorry cannot find password info. Please Try Again" Cancel = -1 Else ' Test to see if the key generated matches the key in           ' the table; if there is not a match, stop the form ' from opening. If Not (rs![keycode] = KeyCode(Cstr(Hold))) Then MsgBox "Sorry you entered the wrong password. " & _ "Please try again.", vbOKOnly, "Incorrect Password" Cancel = -1 End If        End If         rs.Close db.Close Exit Sub

Error_Handler: MsgBox Err.Description, vbOKOnly, "Error #" & Err.Number Exit Sub End Sub </li> Close and then save the Orders form.</li> Open the Orders form and enter PASSWORD when you are prompted for a password.

NOTE: The Orders form opens.</li> Close and then reopen the Orders form and enter PassWord when you are prompted for a password.

Note that you receive the message:

Sorry you entered the wrong password. Please try again.

and the Orders form does not open.</li></ol>

How to Change a Form or a Report Password
You can change the password set on a form or on a report by using the KeyCode function to generate a new KeyCode, and then update the KeyCode for the form or for the report that you want to change the password for. The following example shows you how to change the password for the Orders form from "PASSWORD" to "TestString".
 * 1) Type the following line in the Debug window, and then press ENTER:

?KeyCode("TestString")

The function returns the value 5864, which is the new KeyCode.
 * 1) Open the tblPasswords table in Datasheet view. Remember, the table may be hidden.
 * 2) Find the record that you created where ObjectName = Orders.
 * 3) Type 5864 in the KeyCode field.
 * 4) Close the tblPassword table.

The next time that you open the Orders Form, you must supply the password "TestString" to gain access to the form.

Additional query words: password-protected pass word

Keywords: kbhowto kbprogramming KB179371

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.