Tutorial -- An Access menu / permissions system

SydneyGeek

MrExcel MVP
Joined
Aug 5, 2003
Messages
12,251
This is a post to put in one place an approach I use for giving different access levels to different groups of users in your Access databases. It also provides a flexible menu that adjusts to the user's permissions. It does not require the users to log in: their Windows login name is captured when they open the database. It is easy to restrict the forms. reports etc that they can use, simply by maintaining two tables; one for access levels, and one for menu items.
Parts of this have appeared on this Board before: parts are new. I hope you find it useful.

The login / menu form

You will need:

1. A table called tblStaff, with at least the fields Login and Permissions. Both are Text fields.
(a) In Login, enter the Windows login name of each user who will have ability to add or change data. Those whose names do not appear here will have read-only access to the database.
(b) In Permissions, give them values Edit or Admin

2. A form that opens as your main menu. In this example the form is frmMenu. On this form you need 2 hidden textboxes called txtUser and txtLevel.
In the Form's Load event you will need this code:

If you don't know how to do this, here's how:
Go to the form's Properties.
Click the Events tab.
Double-click the blank Load row, so you see Event Procedure.
Click the Builder (...) button to go to the code window.
Paste this code in, replacing this stuff:
Code:
Private Sub Form_Load

End Sub
With the code you copied.

Here it is:
Code:
Private Sub Form_Load()
    Dim sPermit As String
    Dim iAccess As Integer
    Dim Ctl As Access.Control
    Me.txtUser = Environ("username")
    If IsNothing(Me.txtUser) Then
        sPermit = "ReadOnly"
    Else
        sPermit = GetPermission(Me.txtUser)
    End If
    Select Case sPermit
        Case "Edit"
            iAccess = 2
        Case "Admin"
            iAccess = 3
        Case Else
            iAccess = 1
    End Select
    Me.txtLevel = iAccess
End Sub

Private Function GetPermission(sUser As String)
    If (IsNothing(DLookup("Permissions", "tblStaff", "Login='" & Forms!frmmenu!txtUser & "'"))) Then
        GetPermission = "ReadOnly"
    Else
        GetPermission = DLookup("Permissions", "tblStaff", "Login='" & Forms!frmmenu!txtUser & "'")
    End If
End Function

You give the lowest number to the lowest level of access. So, 1 is for general users whose names do not appear in the login table. They will have read-only access to forms. 2 is for those with more permissions. They will be able to add and edit data. 3 is for those with Admin privileges. They will be able to change user settings and make other behind-the-scenes modifications.

3. On each form that opens you need this in the Form_Load event:

Code:
If forms!frmMenu!txtLevel = 1 Then
   Me.AllowEdits = False
Else
   Me.AllowEdits = True
End If

A listbox to provide the menu items

This listbox will give the users the menu choices they need to navigate through the database. It is based on a table called MenuItems, with these fields:

Code:
Item        Text     The descriptive text that appears in the listbox
Level       Number   1, 2 or 3
Form        Text     The name of the form or report to open
ObjectType  Text     Form or Report
SortOrder   Number   Adjust to suit. List is sorted in ascending order.

The listbox itself is called lstMenu. It lives on the menu form that you created in the first part of this tutorial. It has these properties:

Code:
Row Source Type      Table/Query
Row Source           SELECT MenuItems.Item, MenuItems.Form, MenuItems.ObjectType FROM MenuItems WHERE (((MenuItems.Level)<=Forms!frmmenu!txtLevel)) ORDER BY MenuItems.SortOrder
Column Count         5
Column Widths        3;0;0;0;0
Bound Column         1

Populate the table and build the listbox. Check to see that it works OK. You can do that by giving yourself different levels of access in the login table, closing and re-opening the menu form and checking that the menu adjusts. Now, to make it launch the forms and reports, you will need this code in the Click event of the listbox:

Code:
    Dim sForm As String, sType As String
    sForm = Me.lstMenu.Column(1)
    sType = Me.lstMenu.Column(2)
    Select Case sType
        Case "Form"
            DoCmd.OpenForm sForm
        Case "Report"
            DoCmd.OpenReport sForm, acViewPreview
    End Select


A general function to test for "nothing"
This function below needs to go in a general code module. You will find yourself using it for much more than just the login form.

Code:
Public Function IsNothing(ByVal varValueToTest) As Integer
'-----------------------------------------------------------
' Does a "nothing" test based on data type.
'   Null = nothing
'   Empty = nothing
'   Number = 0 is nothing
'   String = "" is nothing
'   Date/Time is never nothing
' Inputs: A value to test for logical "nothing"
' Outputs: True = value passed is a logical "nothing", False = it ain't
' Created By: John L viescas 01/31/95
' Last Revised: John L viescas 01/31/95
'-----------------------------------------------------------
Dim intSuccess As Integer

    On Error GoTo IsNothing_Err
    IsNothing = True

    Select Case VarType(varValueToTest)
        Case 0      ' Empty
            GoTo IsNothing_Exit
        Case 1      ' Null
            GoTo IsNothing_Exit
        Case 2, 3, 4, 5, 6  ' Integer, Long, Single, Double, Currency
            If varValueToTest <> 0 Then IsNothing = False
        Case 7      ' Date / Time
            IsNothing = False
        Case 8      ' String
            If (Len(varValueToTest) <> 0 And varValueToTest <> " ") Then IsNothing = False
    End Select


IsNothing_Exit:
    On Error GoTo 0
    Exit Function

IsNothing_Err:
    IsNothing = True
    Resume IsNothing_Exit

End Function

Denis
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
any chance someone would know if/how this would work with cascading listboxes? I've got four titles in my main menu that each have their own menu list. I would like to keep the main menu list available and show the choices for the selected category in a new listbox...

Thanks!
 
Upvote 0
Hi Denis,

If you are still monitoring this thread, I would like to ask for your assistance please (or anyone else for that matter).
To preface, I know virtually nothing about how to program in VB. I can kludge my way through some things by standing on the shoulders of giants.
That said, I am trying to build a database and lock down the permissions of various forms. This methodology seems to work quite well, with one exception. I do not want it to be tied to the Windows login.

I need to create three users: Administrator, SuperUser, User. At this point in my database, I have created a Login form that asks for a Username and Password. My tblEmployees has the login, permissions and password fields (which I want). Upon successfully entering the correct credentials, it will bring up the frmMenu (in the example code provided) that lists the other forms, allows users to open them and, depending on the permissions used to log in from the Login form, edit the data (and if they have admin priveleges, be able to edit the listed forms, staff, etc).

I have all of that working, EXCEPT for being tied to the username. Currently, it is tied to the Windows login.

My question is, how would I change/create/code my "Login" form so that it will rely on programmed logins as opposed to my Windows login?

Thank you in advance for any assistance you can provide!
 
Upvote 0
Sorry for the double post - my edit time limit expired.
I wanted to add that the usernames, as I currently have them programmed, are pulled from a combo box. I would assume that in the code to be changed, I would replace the Environ "username" with something that calls the name chosen from the combo box. So more specifically, my request is, how do I do that? Because, I am thinking that, in theory, that would accomplish what I want to do.
Thanks again!
 
Upvote 0
Hi Denis,
I'm hoping you are still monitoring this thread...
I have a new use for restricting access: I want only the users in my table to be the only people even able to view the database, let alone add/edit (I do not want unauthorized users in my database as it is full of very sensitive/personal information). Is there a property setting somewhere in the database for the entire database like there is for each form?
 
Upvote 0
One of the best ways to restrict access to the database is using folder permissions.
Create (or get IT to create) a directory ona shared drive with access limited to only the people who need to see the database, and put your DB into that directory.
Note that the folder access needs to be read/write if the users are going to be able to open the database.

Denis
 
Upvote 0
One of the best ways to restrict access to the database is using folder permissions.
Create (or get IT to create) a directory ona shared drive with access limited to only the people who need to see the database, and put your DB into that directory.
Note that the folder access needs to be read/write if the users are going to be able to open the database.

Denis

Works in theory, but our company is so big with many regional offices there is a policy regarding the technology..... there are only so many folders allowed with restrictions.... it would also be a pain to contact IT when personnel changes as they are not in either of our offices....

Is there a second best way to restrict access? Can I just use the user ID as mentioned in this thread in some way?

THANKS!
 
Upvote 0
You could do this a couple of ways.
1. Use a table that has the user logins (like the current model). When the database launches, run a routine that looks for the user's login in that table. If not found, display a message and quit.
2. Build a password form that launches when the user opens the DB. Wrong password and / or user login means no entry; quit the database.

Which would you prefer to use?
Denis
 
Upvote 0
Choice 1 for sure! The easier the better for my not-so-tech savvy group :)
I was acutally just looking into the use of the personalized splash screen....
 
Upvote 0
Hi, I've just started using Access 2010 and am trying to get tyour code to work. Thanks for the code, btw. :)

Sometimes the Items from MenuItems are listed and sometimes there is nothing... any ideas?

Thanks, in advance, for any help you can provide.
 
Upvote 0

Forum statistics

Threads
1,215,398
Messages
6,124,688
Members
449,179
Latest member
kfhw720

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top