Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Access Level to open form

Status
Not open for further replies.

Brianfree

Programmer
Feb 6, 2008
220
0
0
GB
Hi i have the following code which i found and works well, however i have some forms on the switchboard that i only want certain people to access. There is a field in the table called level which is eith set to manager, user, or admin.

If a user logs in and has a level of user he/she can only access certain forms if he tries to access a form which has a level of manager a warning pops up.. How can i do this??

many thanks

Brian

Code:
Private Sub cmdLogin_Click()

'Check to see if data is entered into the UserName combo box

    If IsNull(Me.cboEmployee) Or Me.cboEmployee = "" Then
            MsgBox "You must enter a User Name.", vbOKOnly, "Required Data"
            Me.cboEmployee.SetFocus
        Exit Sub
    End If

'Check to see if data is entered into the password box

    If IsNull(Me.txtPassword) Or Me.txtPassword = "" Then
            MsgBox "You must enter a Password.", vbOKOnly, "Required Data"
            Me.txtPassword.SetFocus
        Exit Sub
    End If

'Check value of password in tblEmployees to see if this matches value chosen in combo box

    If Me.txtPassword.Value = DLookup("strEmpPassword", "tblEmployees", "[lngEmpID]=" & Me.cboEmployee.Value) Then

        lngMyEmpID = Me.cboEmployee.Value

'Close logon form and open splash screen
        
        DoCmd.Close acForm, "frmLogon", acSaveNo
        DoCmd.OpenForm "frmSplash_Screen"

        Else
        MsgBox "Password Invalid.  Please Try Again", vbOKOnly, "Invalid Entry!"
        Me.txtPassword.SetFocus
    End If
    
'If User Enters incorrect password 3 times database will shutdown
    
    intLogonAttempts = intLogonAttempts + 1
    If intLogonAttempts > 3 Then
        MsgBox "You do not have access to this database.  Please contact your system administrator.", vbCritical, "Restricted Access!"
        Application.Quit
    End If
    
End Sub
 
You've got the code right there !

Save the users data (from theDLookup) to a public variable in a module.

In the on_open event of the forms you want to open, use pretty much the same code you provided as an example. If it doesn't match, display a message box or something and immediately close the form.



Tyrone Lumley
SoCalAccessPro
 
Be aware that you may lose any global variables if you have an unhandled error. As I understand, you are getting the user name from the environment or an API, if this is the case, I suggest you dlookup the user in the open event of the form and use the cancel option if the user does not have the right access level. Another possibility would be to diable menu items based on the dlookup result.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top