HOWTO: ACC: Create a Password Protected Form or Report

Exención de responsabilidades de contenido KB retirado

Este artículo se refiere a productos para los que Microsoft ya no ofrece soporte técnico. Por tanto, el presente artículo se ofrece "tal cual" y no será actualizado.

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


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:

  1. Open the sample database Northwind.mdb.
  2. 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
  3. 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
  4. Open the tblPassword table and enter the following data:

    ObjectName: Orders
    KeyCode: 2818
  5. Open the Orders form in Design view.
  6. 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
    ' 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
    Exit Sub

    MsgBox Err.Description, vbOKOnly, "Error #" & Err.Number
    Exit Sub
    End Sub
  7. Close and then save the Orders form.
  8. Open the Orders form and enter PASSWORD when you are prompted for a password.

    NOTE: The Orders form opens.
  9. 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.

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:


    The function returns the value 5864, which is the new KeyCode.
  2. Open the tblPasswords table in Datasheet view. Remember, the table may be hidden.
  3. Find the record that you created where ObjectName = Orders.
  4. Type 5864 in the KeyCode field.
  5. 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.

Id. de artículo: 179371 - Última revisión: 01/22/2007 - Revisión: 1