Results 1 to 10 of 10
  1. #1
    ludovic_44 is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Apr 2014
    Location
    mauritius
    Posts
    15

    Talking Enable and disable button based on user login form


    Views: 29 Size: 1.19 MB">Master_Netw.zip


    Hi all,

    I have a login form with users and password in table user. userlogin and password are the same except for lrambhujun which is "carabine"
    i have created a Dashboard with button on the left where you can click to select other button on it's right

    I would like to enable and disable some buttons based on the user login

    I have google but could ot understant how to do it, can you please help me, i have already attached one database

    If you can do one for the first button only and i will proceed with next

    But i dont know how to solve this issue

    Please have a look and help me

    Thanks for your precious help

    Ludovic

  2. #2
    Ruegen's Avatar
    Ruegen is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jul 2013
    Location
    Australia
    Posts
    1,496
    Quote Originally Posted by ludovic_44 View Post
    Master_Netw.zip

    Hi all,

    I have a login form with users and password in table user. userlogin and password are the same except for lrambhujun which is "carabine"
    i have created a Dashboard with button on the left where you can click to select other button on it's right

    I would like to enable and disable some buttons based on the user login

    I have google but could ot understant how to do it, can you please help me, i have already attached one database

    If you can do one for the first button only and i will proceed with next

    But i dont know how to solve this issue

    Please have a look and help me

    Thanks for your precious help

    Ludovic
    each button is a control

    each control has an event action properties

    So using an if statement in VBA can allow you to check if you have the correct user.

    You could lock the button when they click the button (click event) or (and do an if statement) or you could do something more global like lock the buttons when the form loads (form open event)

    You can store the values from the fields that have been entered (correctly) into public variables - make a module, make two public variables and store the value of the name field and in another variable store the password.

    That way you can call the two variables in the IF statement whenever you need it.

    I would even go a step further by making a public variable at the start which is a bool (true or false) and call the variable "Admin"

    that way you don't need to call two variables, just one - admin.

    If they are an admin then the button does the action, else it does nothing.

  3. #3
    Ruegen's Avatar
    Ruegen is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jul 2013
    Location
    Australia
    Posts
    1,496
    In the module
    Code:
    public Admin as bool
    on the button
    Code:
    if Admin = true then 'checks to see if admin variable is true or false
    
    docomd.open "formname" 'opens a from
    
    else 'this is so that they get feedback as to why the button didn't do anything
    
    msgbox "You are not an admin, this will form will not open"
    
    end if

  4. #4
    ludovic_44 is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Apr 2014
    Location
    mauritius
    Posts
    15
    i'm not a pro in access, but really i dont catcht it,

    can you please try to explain it to me more clearly please

    Ludovic

  5. #5
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    I took a look at your DB

    My approach would be to use the Command Buttons' Tag property. In the property I would put something like
    123
    or
    3
    or
    13

    Type the value of the access level you want for each command button in its respective tab property. The tag property can be found in the Other tab of the property sheet. I would also make sure that the user's access level is stored in a global variable or in a textbox on the form. I noticed that your table has AccessLevelID as text type. Perhaps integer is more appropriate.

    You could have a global variable of integer named something like gintAccessLevelID

    At the top of your form's module, I would make a declaration in the modules header.
    Option Compare Database
    Dim strTag As String


    Then, each click event for the Command Button's would look something like this.....

    Code:
    strTag = ""
    strTag = Me.Command47.Tag
        If InStr(strTag, gintAccessLevelID) Then
            Me.frmDevelopment.SourceObject = "frmDevelopment"
        Else
            MsgBox "You do not have access to this form", vbInformation, "No Access"
        End If
    Another approach would be to use the Tag property to adjust the Enabled property of each Command button during the form's load event. For this, you would default the relevant controls to disabled. You would then place some code in the form's load event to enable the controls allowed based on the user. This example does not use the strTag declaration in the form's module.


    The form's load event would be something like

    Code:
    Dim ctl As Control
    For Each ctl In Me.Controls
        If InStr(ctl.Tag, gintAccessLevelID) Then
            ctl.Enabled = True
        End If
    Next

  6. #6
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    The first thing you need to do is make the current User's Access level available to the form named "frmDashboard". You can do this various ways. You can store the value in a global variable using a standard module. Another approach would be to pass the value to the dashboard form via an open arg. Here is an example of the code you would use in frmLogin to include an open arg equal to the value of AccessLevelID.


    Code:
    If IsNull(Me.txtLoginID) Then
        MsgBox "Please enter Login ID", vbInformation, "Login ID Required"
        Me.txtLoginID.SetFocus
        ElseIf IsNull(Me.txtPassword) Then
        MsgBox "Please enter password", vbInformation, "Password Required"
        Me.txtPassword.SetFocus
    Else
    If (IsNull(DLookup("[txtuserlogin]", "tblUser", "[txtUserlogin] ='" & Me.txtLoginID.Value & "' And [txtpassword] = '" & Me.txtPassword.Value & "'"))) Then
    MsgBox "Incorrect Login ID or Password"
    Else
    Dim intAccess As Integer
    intAccess = DLookup("[AccessLevelID]", "tblUser", "[txtUserlogin] ='" & Me.txtLoginID.Value)
    DoCmd.OpenForm "frmdashboard", , , , , , intAccess
    DoCmd.Close acForm, "frmlogin"
    
        End If
        End If
    From there, you can safely store the open arg in frmDashboard for future reference using an unbound textbox. In the Load event of frmDashboard

    Me.UnboundTextBox.Value = Me.OpenArgs

  7. #7
    ludovic_44 is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Apr 2014
    Location
    mauritius
    Posts
    15
    Hi,

    thanks for your great help, i think im becomming crazy, as said someone, too much code kills, loll

    it gives me an error " Syntax erro in string in querry expression '[txtuserlogin]='lrambhujun'. "
    Do you know why it says this error

    Thanks

    you are realy a great help

    Ludovic

  8. #8
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    If you get an error, it is helpful to post the code you are using and indicate what linr the debugger halts on.

    I will assume that the following is the line causing the error.
    intAccess = DLookup("[AccessLevelID]", "tblUser", "[txtUserlogin] ='" & Me.txtLoginID.Value)

    I made a mistake and did not concatenate correctly.
    try
    intAccess = DLookup("[AccessLevelID]", "tblUser", "[txtUserlogin] ='" & Me.txtLoginID.Value) & "'"

  9. #9
    burrina's Avatar
    burrina is offline VIP
    Windows 8 Access 2010 32bit
    Join Date
    Oct 2012
    Location
    Freeport,Texas
    Posts
    1,383
    Hi Ludovia. I think your Security approach is flawed by design personally. How are are you using the UserLevelID to set their level of Security? On each forms OnOpenEvent, the users security should be enforced. Am I missing something , do you have a Global Module or code to handle this?

    EDIT: oops, too slow. I see this is being addressed.
    Last edited by burrina; 05-04-2014 at 12:49 PM. Reason: Toos Slow

  10. #10
    burrina's Avatar
    burrina is offline VIP
    Windows 8 Access 2010 32bit
    Join Date
    Oct 2012
    Location
    Freeport,Texas
    Posts
    1,383
    Did you get this Resolved?

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

Similar Threads

  1. Replies: 3
    Last Post: 12-02-2012, 09:38 AM
  2. Replies: 2
    Last Post: 11-30-2012, 08:03 PM
  3. Enable/Disable Button
    By P.Malius in forum Programming
    Replies: 3
    Last Post: 09-07-2012, 08:36 AM
  4. Replies: 3
    Last Post: 08-02-2012, 10:27 AM
  5. Replies: 1
    Last Post: 02-25-2011, 10:03 AM

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