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
 
Have you set up a user or two in the database? Denis

Yes, I did that when I first set up the table.


If so, go into the code and put a break at Denis

I understand before or after, but what does break at mean? Do you mean I should insert a blank line somewhere?

the first line of code for the Form_Open event on the menu form. Denis

In using the code you gave on page 1, I can 't find Form_Open


You can bypass the opening code by holding down the {SHIFT} key as the database launches. That lets you go into the tables, set up one or two users, and then launch again. Denis


Are we speaking of the first database you instructed us to make? The instructions never mentioned doing anything to cause the need to open this way. Thanks so much for your help.</SPAN>



Thanks so much for your help.</SPAN>
 
Last edited:
Upvote 0

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
I'm loving the idea of this feature. A great way to save the database from unintended editing.

As a general user of our office computers creating this, I am, however, at a loss for how to find my Windows Login (and that of my co-workers for that matter) as we have "Log in" ID's that are not our names-but when I look where instructed after doing a google search I see my lastname, firstname as the Windows ID......

2nd, after starting the tutorial--the code error I come across is the GetPermission of the first code listed. I'm assuming this has something to do with my Windows ID.

thanks again for this awesome idea & instructions!
 
Upvote 0
Hi, in answer to your questions:

Use the ID you log in with. There is a function that gets your Windows login and compares it to the table that holds your permissions.
You need to edit that table, entering the Windows login name and permission level for each user.
BTW, you can hold down the SHIFT key when the database launches, to prevent the code from running. That way you can edit the user table, then close and reopen the database.

Denis
 
Upvote 0
Use the ID you log in with. There is a function that gets your Windows login and compares it to the table that holds your permissions.
You need to edit that table, entering the Windows login name and permission level for each user.
BTW, you can hold down the SHIFT key when the database launches, to prevent the code from running. That way you can edit the user table, then close and reopen the database.

Okay, I've got my login changed to my ID.... must be doing something else wrong tho as I have nothing populating in my listbox, at all.... :(
 
Upvote 0
my on load for the main form is also throwing errors in code: (the red text is where the code is highlighted)
Rich (BB 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
Me.lstMenu.Requery
 
Upvote 0
The code for the form has 2 routines. One is Form_Load. The second is GetPermission. Do you have that function in your code? Here it is again...
Code:
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

The other thing is that all of the fields, tables and control names must match what you have in the code.
Denis
 
Last edited:
Upvote 0
All table & form names matched. After reviewing your initial tutorial several times (line by line :)) I found I had missed the "Get Permission" code entirely. I added that and the listbox now populates :D

Any idea of how to limit user ability to modify the layout, format and anything BUT data on forms to prevent inadvertant modification of the database function? I've tried the login as other user ability and find that I can still click on the different views of reports and forms to be able to edit them (even with user level 1). This was my purpose of restricting the database in the first place (I've got users that get click happy and don't realize what they are doing until everything is messed up and I'm left to figure out how to fix it)

THANKS!
 
Upvote 0
Presumably you have split the database. If so, create an .accde or .mde (save the original .accdb / .mdb so you can do design work yourself) and give that to your users.
The .accde / .mde is a compiled version, and all forms, reports and VBA are protected from design changes. Queries and Macros are not.

Also, you should ALWAYS have a backup copy of your database in case someone mangles it for you.

Denis
 
Upvote 0
Thanks.
I was hoping to not have to split it, but if I have to in order to get what I want/need.....

All split and protected (with three copies in different areas for protection...)
Thanks again for everything!
 
Upvote 0
I understand the reluctance to split the DB because it's easier to develop the original with everything in one file. But once you go multi-user, it's *much* safer to split the database, and it becomes a lot easier to make design changes too because you don't have to boot users out while you work on a development copy.
Glad to hear it's sorted.

Denis
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,636
Latest member
laura12345

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