登录表单,具有访问权限

时间:2014-02-08 23:58:22

标签: access-vba

我使用登录表单创建了一个简单的数据库。

我有一个带有AccessLevelID(autonumber)和Accless Level的tblAccessLevel,与用户表“tblUsers”有关系 添加新用户后,您可以选择所需的访问级别。

我试图添加规则,以便在打开某些表单时,它会查看登录的用户,看看他们是否具有正确的访问级别,然后允许或拒绝它们。

这是我的代码:

UserView = DLookup("AccessLevel", "tblUsers", "[curUser] = " & [AccessLevel])
If UserView = "1" Then
Me.Command0.Enabled = True
Else: UserView = "3" Or "2"
Me.Command0.Enabled = False
End If

curUser在登录表单中定义:

curUser = DLookup("Username", "tblUsers", "[Username]=""" & Me.txtUsername.Value & """")

亲切的问候, 阿什利

Main_Form

Private Sub Form_Load()
Set dbLog = CurrentDb
Set LogRec = dbLog.OpenRecordset("tblLog")
LogRec.AddNew
LogRec("eDate").Value = Date
LogRec("eTime") = Format(Now, "Long Time")
LogRec("Form").Value = "Main Form"
LogRec("User").Value = curUser
LogRec("Detail").Value = curUser & " Opened Main Form"
LogRec.Update

UserView = DLookup("AccessLevel", "tblUsers", "[curUser] = " & [AccessLevel])

If UserView = 1 Then
Me.Command0.Enabled = True
Else
Me.Command0.Enabled = False
End If

End Sub

Form_Login(带代码的登录命令)

Public Sub cmdLogin_Click()
Dim dbLog As DAO.Database
Dim LogRec As DAO.Recordset

'Check to see if data is entered into the Username Field
If IsNull(Me.txtUsername) Or Me.txtUsername = "" Then
    MsgBox "You must enter a Username.", vbOKOnly, "Required Data"
    Me.txtUsername.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 tblEmplyees to see if this matched value chosen in Username Field

If Me.txtPassword.Value = DLookup("Password", "tblUsers", "[Username]=""" & Me.txtUsername.Value & """") Then
curUser = DLookup("Username", "tblUsers", "[Username]=""" & Me.txtUsername.Value & """")

Set dbLog = CurrentDb
Set LogRec = dbLog.OpenRecordset("tblLog")
LogRec.AddNew
LogRec("eDate").Value = Date
LogRec("eTime") = Format(Now, "Long Time")
LogRec("Form").Value = "Login"
LogRec("User").Value = curUser
LogRec("Detail").Value = "User " & curUser & " Logged in"
LogRec.Update


MyEmpID = Me.txtUsername.Value

'Close Logon form and open splash screen

DoCmd.Close acForm, "frmLogin", acSaveNo
DoCmd.OpenForm "MainForm"

Else

Set dbLog = CurrentDb
Set LogRec = dbLog.OpenRecordset("tblLog")
LogRec.AddNew
LogRec("eDate").Value = Date
LogRec("eTime") = Format(Now, "Long Time")
LogRec("Form").Value = "Login"
LogRec("User").Value = "N/A"
LogRec("Detail").Value = "Someone tried to access your database with an invalid password"
LogRec.Update


MsgBox "Password invalid. Please try again", vbCritical + vbOKOnly, "Invalid Entry!"
Me.txtPassword.SetFocus

End If

'If User Enters incorrect password 3 times database will shutdown
intLogonAttempts = intLogonAttempts + 1
If inLogonAttempts > 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




Private Sub Form_Load()
Set dbLog = CurrentDb
Set LogRec = dbLog.OpenRecordset("tblLog")
LogRec.AddNew
LogRec("eDate").Value = Date
LogRec("eTime") = Format(Now, "Long Time")
LogRec("Form").Value = "Login"
LogRec("User").Value = "N/A"
LogRec("Detail").Value = "Someone logged into your database"
LogRec.Update
End Sub

ModCurUser

Option Compare Database
Public curUser As Variant

ModUserView

Option Compare Database
Public UserView As Variant

错误出现在:

UserView = DLookup("AccessLevel", "tblUsers", "[curUser] = " & [AccessLevel])

1 个答案:

答案 0 :(得分:0)

假设'curUser'是全局变量,可供所有VBA模块使用,您也可以将'UserView'设为GV。然后,是的,您可以根据“UserView”启用或禁用“切换板”命令按钮。如果您担心创意人员只需手动打开表单,那么您可以在表单中添加代码,如:

Private Sub Form_Load()
If UserView = 1 Then
    ' no action necessary - allow access
Else
    MsgBox "You do not have permission to use this form"
    DoCmd.Close acForm, Me.Name
End If
End Sub

您可能感兴趣的其他一些观察结果:

  1. 变量'intLogonAttempts'需要在Form级别定义 - 而不是在子程序内定义。如果没有,它将永远不会正确计算尝试次数(将始终为1)。
  2. 当您加载登录表单时,您说'N / A'正在尝试登录。最好跟踪用户登录名或工作站名称,即Environ $(“Username”)或Environ(“computername” “)
  3. 在比较验证密码时,某些应用程序可能希望强制进行100%的比较。目前,您的测试不会强制用户匹配密码的CASE。以下是一个简单的解决方案:

    If Len(Me.txtPassword.value) = Len(<from your Dlookup>) Then
        For i = 1 To Len(<from your Dlookup>)
            If Asc(Mid$(Me.txtPassword.value, i, 1)) <> Asc(Mid$(<from your Dlookup>, i, 1)) Then
                blnWrongPWD = True
            End If
        Next i
    Else
        blnWrongPWD = True
    End If
    
  4. 祝你好运,Wayne