- database solutions and downloads for microsoft access

Password Protect Buttons in your Microsoft Access Forms

Add Password Security to a Microsoft Access Command Button

QUESTION » I'm new to Microsoft Access and I'm currently working in version '97. My question is this; I have a main form on my application with command buttons to navigate through the different sections of my application. What I'd like to do is secure one of the command buttons and restrict access to only the manager of the department.

SOLUTION » Security is a very large subject relating to Microsoft Access. There are many, many different ways to implement security and ensuring that your users have access to only what they are allowed to see.

You can apply Microsoft Access User and Group Security measures and add rights to each group to allow different levels. I would advise that you take a good read of this from the Access Help files (Applying User and Group security Permissions) and always ensure that you back up your files before attempting any of this.

The following is a very simple approach to enabling only a user who has the password to access what is behind a command button.

If a user knows how to bypass the hidden database window then this will require much more work You will need to set start-up options, hide toolbars and disable the shift bypass key.

Below shows the switchboard form that will have the security attached:

Example Switchboard

Suppose that you wish to prevent certain users from accessing the Employee form as this form may contain confidential information...

Open the switchboard form in design view and select the command button for the Employee form (cmdOpenEmpForm)

Properties Window for cmdOpenEmpForm
Properties Window for cmdOpenEmpForm

Select the On Click event and choose Event Procedure from the drop down list. Click on the ellipses (...) to enter the code window.

'***************** Code Start ***************
Private Sub cmdOpenEmpForm_Click()

'Attached to On Click event of cmdOpenEmpForm

    Dim strPasswd

    strPasswd = InputBox("Enter Password", "Restricted Form")

    'Check to see if there is any entry made to input box, or if
    'cancel button is pressed. If no entry made then exit sub.

    If strPasswd = "" Or strPasswd = Empty Then
        MsgBox "No Input Provided", vbInformation, "Required Data"
        Exit Sub
    End If

    'If correct password is entered open Employees form
    'If incorrect password entered give message and exit sub

    If strPasswd = "Graham" Then
        DoCmd.OpenForm "frmEmp", acNormal
        MsgBox "Sorry, you do not have access to this form", _
               vbOKOnly, "Important Information"
        Exit Sub
    End If
End Sub
'***************** Code End ***************

You will notice the line If strPasswd = "Graham" Then . This contains the required password that will be required when attempting to open the form.

This can be changed to any password that you wish.

Now when you click on the Open Employees command button on the switchboard you will see the following input box open:

Input Box for entering the password

If the correct password is entered the form will open. However, if the incorrect password is entered you will receive the following message:

Input Box Error Message
Input Box Error Message