Results 1 to 9 of 9
  1. #1
    Emmanuel is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Jan 2020
    Posts
    272

    Diffrernt access level privileges

    I have a table with name tbl_login with fields:

    UserID
    FirstName
    LastName
    UserName
    Password


    I also have a form with name frm_login with textboxes:

    One for Username with textbox name : txt_username

    the other for passowrd with textbox name : txt_password


    i have a command button with name cmd_login with the following codes behind it.



    Dim db As DAO.Database
    Dim rst As DAO.Recordset
    Dim strSQL As String

    If Trim(Me.txt_username.Value & vbNullString) = vbNullString Then
    MsgBox prompt:="Username should not be left blank.", buttons:=vbInformation, title:="Username Required"
    Me.txt_username.SetFocus
    Exit Sub
    End If

    If Trim(Me.txt_password.Value & vbNullString) = vbNullString Then
    MsgBox prompt:="Password should not be left blank.", buttons:=vbInformation, title:="Password Required"
    Me.txt_password.SetFocus
    Exit Sub
    End If

    'query to check if login details are correct'
    strSQL = "SELECT FirstName FROM tbl_login WHERE Username = """ & Me.txt_username.Value & """ AND Password = """ & Me.txt_password.Value & """"

    Set db = CurrentDb
    Set rst = db.OpenRecordset(strSQL)
    If rst.EOF Then
    MsgBox prompt:="Incorrect username/password. Try again.", buttons:=vbCritical, title:="Login Error"
    Me.txt_username.SetFocus
    Else


    MsgBox prompt:="Hello, " & rst.Fields(0).Value & ".", buttons:=vbOKOnly, title:="SOFTWARE"
    DoCmd.OpenForm "A"



    I will like to create a new field in tbl_login with the name Access_level which will contain the following user access permissions;

    Administrator
    Accounts
    Secretary


    I will like my code to be re modified such that:

    when login with access_level administrator, it should open form A
    when login with access_level Accounts, it should open form B
    when login with access_level Secretary, it should open form C


    Any help with this will be greatly appreciated

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,525
    Along with userid and password is the field Level. (or call it whatever)

    when user opens the db, the main menu form will open and grab the userID.
    Then lookup that persons rights in order to enable/disable controls.
    put the UserId into an invisible text box on the form,txtUserID , so queries can see it.


    Code:
    public gvUserID 
    
    
    sub form_load()
    dim vLevel
    
    
     gvUserID =  Environ("Username")      'get userID,visible in all forms
    
    
    'get level from user table
       vLevel = Dlookup("[Level]","tUsers","[userID]='" & gvUserID & "'")
    
    
    'now, enable/disable items on form
       select case vLevel
             case "A"  'admin 
                 'all is enabled
    
    
             case "U"  'normal user
                 txtBox1.enabled = false
                 txtManager.enabled = false
    
    
             case "M"  'manager
                 txtBox1.enabled = false
       end select
    end sub

  3. #3
    Emmanuel is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Jan 2020
    Posts
    272
    Quote Originally Posted by ranman256 View Post
    Along with userid and password is the field Level. (or call it whatever)

    when user opens the db, the main menu form will open and grab the userID.
    Then lookup that persons rights in order to enable/disable controls.
    put the UserId into an invisible text box on the form,txtUserID , so queries can see it.


    Code:
    public gvUserID 
    
    
    sub form_load()
    dim vLevel
    
    
     gvUserID =  Environ("Username")      'get userID,visible in all forms
    
    
    'get level from user table
       vLevel = Dlookup("[Level]","tUsers","[userID]='" & gvUserID & "'")
    
    
    'now, enable/disable items on form
       select case vLevel
             case "A"  'admin 
                 'all is enabled
    
    
             case "U"  'normal user
                 txtBox1.enabled = false
                 txtManager.enabled = false
    
    
             case "M"  'manager
                 txtBox1.enabled = false
       end select
    end sub
    Am kind of lost with your explanations here

  4. #4
    Emmanuel is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Jan 2020
    Posts
    272
    Would be glad if I could get help with the select if creteria

  5. #5
    orange's Avatar
    orange is offline Moderator
    Windows 10 Office 365
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726

  6. #6
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,803
    Is form A, B and C all the same except for what the user has permissions to? I wouldn't do that - it's 3x the work if you have to change something like how the form opens.
    You don't put 3 values in the field in the user table. If these levels are accumulative from least to most (i.e. level3 would have all, level2 has permissions of 1 and 2 and level1 only has basic permissions) I would do something like (where Admin has all permissions)
    tblLevels
    LevelID LevelOrder Level Description
    1 5 User basic
    2 10 Manager supervisory
    3 15 Admin all

    To provide accumulative access to 2 levels on a form, WHERE LevelOrder < 11. To provide non-contiguous, WHERE LevelOder=5 OR LevelOrder=15 etc.
    Your code could be simplified as DLookups rather than using a recordset like Ranman did. Gaps between these values allows for inserting future levels in between others.

    If you could expand on what you didn't follow about that post we can probably add some clarity.
    BTW, unless people who use the db share a Windows login, there is a way to avoid all this password stuff and keep everyone out. tblUser would have user LoginID. An invalid users id would not be found in the table, thus they wouldn't get in unless you left loopholes. In that case, a password routine is no better.

    Forgot to mention that the autonumber field value from tblLevels is what gets stored in the user table.
    Last edited by Micron; 05-24-2021 at 12:01 PM. Reason: added info
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  7. #7
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,803
    13 minutes into the vid but have to pause for now. Anyone else wonder why you'd validate a user in a form load event rather than the open event? Open occurs first. User is invalid for that form. Now you have to close the form, or you had to make it invisible first. Why not check it in the open event, which can be cancelled?
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  8. #8
    Join Date
    Apr 2010
    Location
    Steamboat Springs
    Posts
    2,529
    Here is tutorial that I used on several projects. It is a bit dated, but it did work for me for permissions.

    https://www.mrexcel.com/board/thread...system.248191/

  9. #9
    Emmanuel is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Jan 2020
    Posts
    272
    Have been able to find a fix. Thanks to you all for your time

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 9
    Last Post: 08-18-2020, 02:32 AM
  2. Linking Tables at the Table Level Vs. the Query Level
    By Tammy in forum Database Design
    Replies: 3
    Last Post: 12-03-2014, 01:34 PM
  3. need to add Privileges..
    By Stephanie53 in forum Forms
    Replies: 8
    Last Post: 05-22-2013, 11:41 AM
  4. Loss of privileges After Compact
    By jonsuns7 in forum Security
    Replies: 1
    Last Post: 02-01-2010, 05:43 AM
  5. Privileges problems whe executing a query
    By admaldo in forum Security
    Replies: 0
    Last Post: 04-27-2006, 07:22 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums