MS Access Object Security

Today’s post is based on a question that I received.

We are going to assume that there are multiple users who have rights
to view different database objects.

So in this example there are 3 user logins with different rights to access 3 forms and 3 reports.

Here is an overview of the database setup:

MS_Access_Object_Security_1

This is tblLogins:

MS_Access_Object_Security_2

This is tblAdmin:

MS_Access_Object_Security_3

Notice that all you really need to do is check whether the user has access to the object.

If you want, you can assign the permissions based on groups, instead of user by user. Then you can assign the users to groups, and the groups can have the permissions to the objects.

On the “Open” event for each form, place the following code:

Private Sub Form_Open(Cancel As Integer)
    If Not Allowed(Me.Name) Then
        MsgBox "You are not allowed here"
        Cancel = True
    End If
End Sub

On the “Open” event for each report, place the following code:

Private Sub Report_Open(Cancel As Integer)
    If Not Allowed(Me.Name) Then
        MsgBox "You are not allowed here"
        Cancel = True
    End If
End Sub

On the “Open” event of each database object we are using the “Allowed” function to let us know if the user is allowed to open the database object.

The “Allowed” function is used by a number of forms and reports, so we put the “Allowed” function in a module called “basSecurity” by itself.

Here is the “Allowed” function:

Public Function Allowed(ObjectName) As Boolean
    Dim rst As Recordset
    Dim strSQL As String

    
    strSQL = "SELECT Allowed FROM tblAdmin WHERE ObjectName='" & ObjectName & "' AND UserName ='" & m_LoggedInUser & "'"
        
    Set rst = CurrentDb.OpenRecordset(strSQL)
    
    If rst(0) = True Then
        Allowed = True
    Else
        Allowed = False
    End If
    
    rst.Close
    Set rst = Nothing
    
End Function

This function will check the tblAdmin table to see if the user is allowed to open the requested object.

basSecurity also has a modular scope variable called “m_LoggedInUser”, to store the user name of the currently logged in user.

Here is the screenshot all together:

MS_Access_Object_Security_4

To test and open all the forms and reports I am using a form called “frmMain”:

MS_Access_Object_Security_5

…and here is “behind the scenes”:

MS_Access_Object_Security_6

That’s about all, let me know if you have any questions.




By the way, if you got or are getting value from the VBA information, please give me a tip, thanks!


These posts may help answer your question too...

How can I interact with other Office applications (Excel) using VBA in Access?

Need to write your Access data or query to an Excel file? Here is the how to do it: Most people are familiar with Excel and know how to use it well (enough), and when you start talking about Access, they get scared off, and don’t know what to do anymore. Well, here you are […]

How To Generate A XML File With Access VBA

XML is used to structure your data. You can send a CSV file to another person, but using XML, you can structure your data in a more elegant format, and get very specific about your fields, and what data is contained within your tags (your field names). This is different than a basic CSV file […]

Mouse Click Counter On Access Form

This post will demonstrate how you can count the number of clicks on your button in a certain time frame. It will function like a game . Here is the database form all in one: Option Compare Database Public m_dteStartTime As Date Public m_dteStopTime As Date Private Sub btnClicker_Click() Dim intValue As Integer Dim rst […]

Shared Access Database Management

Here is a handy setup for those who need to manage a shared access database. Either you can create this from scratch as per the example or add the new tables to your existing database. This setup will track: 1. the users currently using the database 2. what time the user and computer name currently […]

Previous Post

VBA MS Access Form Filter Example

Next Post

Here Is A Customized Msgbox VBA Example