vba Microsoft Access 登录系统和管理员用户级安全
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13397322/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Microsoft Access Login System & Admin User Level Security
提问by user1819465
before you ask i have spents weeks googling this to no avail. I am currently designing a database system for a business in Microsoft Access 2007. The system works fine but theres some things i need help with to fix.
在你问之前,我已经花了数周的时间在谷歌上搜索无济于事。我目前正在为 Microsoft Access 2007 中的企业设计数据库系统。该系统工作正常,但我需要帮助修复一些问题。
Currently, i have 2 forms;
目前,我有 2 个表格;
Login_FRM - A form which is a login screen. The code for this box at the moment is
Login_FRM - 作为登录屏幕的表单。目前这个盒子的代码是
If IsNull(Me.Username) Or Me.Username = "" Then
MsgBox "You must enter the username of an active employee.", vbOKOnly, "Required Data"
Me.Username.SetFocus
Exit Sub
End If
If IsNull(Me.PasswordLookup) Or Me.PasswordLookup = "" Then
MsgBox "You must provide a password.", vbOKOnly, "Required Data"
Me.PasswordLookup.SetFocus
Exit Sub
End If
Me.PasswordLookup.Value = DLookup("[Password]", "User_TBL", "[Username] ='" & Me.Username & "'")
If Me.PasswordLookup.Value = Me.PasswordLookup.Value Then
DoCmd.OpenForm "MainMenu_FRM"
Else
MsgBox "Incorrect Username or Password. Please try again.", vbOKOnly, "Please try again"
Me.PasswordLookup.SetFocus
Exit Sub
End If
End Sub
This checks if null etc, but also checks against fields stored in the User_TBL to see if the user can gain access.
这会检查是否为 null 等,还会检查存储在 User_TBL 中的字段以查看用户是否可以获得访问权限。
I also have MainMenu_FRM, which is a main springboard for all the processes of the database.
我还有MainMenu_FRM,它是数据库所有进程的主要跳板。
Basically what i want to do is to ONLY allow a user access to an Admin menu, if there are registered as Admin in the User_TBL (Yes/No)
基本上我想要做的是只允许用户访问管理员菜单,如果在 User_TBL 中注册为管理员(是/否)
I would be very greatful for any help that anyone has.
我会非常感谢任何人的任何帮助。
Thanks
谢谢
Here is a quick outline of the whole process
这是整个过程的快速概述
User /who is not admin/ is displayed with login screen.
User enters details and clicks login, gained access to main menu.
Clicks admin button on main menu. Is denied access.
用户 /who is not admin/ 与登录屏幕一起显示。
用户输入详细信息并单击登录,即可访问主菜单。
单击主菜单上的管理按钮。被拒绝访问。
回答by HelloW
I think that it may be best to present everyone with a log in screen and then check if they are Admin when they try to access the Admin screen.
我认为最好向每个人展示登录屏幕,然后在他们尝试访问管理屏幕时检查他们是否是管理员。
回答by Seth E
I think what you need to do is simpler than you think.
我认为您需要做的事情比您想象的要简单。
Just do an if statement in the form load of the MainMenu_FRM
that only enables the buttons for admins and set the default so that it's disabled for everyone else. Then only admins can click it.
只需在表单加载中执行 if 语句,该语句MainMenu_FRM
仅为管理员启用按钮并设置默认值,以便对其他人禁用。然后只有管理员可以点击它。
Why don't admins have to login as well? Just have them login, and set their permissions appropriately after they log in.
为什么管理员也不必登录?只需让他们登录,并在他们登录后适当地设置他们的权限。
i.e. Set every button on MainMenu_FRM
to Enabled = no
in properties.
即设置在每个按钮MainMenu_FRM
来Enabled = no
的属性。
Private Sub Form_Load()
'****Admin****
If strUserAccess = "Admin" Then
Me.buttonThatTakesYouSomewhere.Enabled = True
Me.buttonThatTakesYouSomewhereElse.Enabled = True
'****USER****
ElseIf strUserAccess = "User" Then
Me.buttonThatTakesUsersSomewhere.Enabled = True
Me.buttonThatTakesUsersSomewhereElse.Enabled = True
end if
end sub
Also, for security reasons, make sure you password your code and disable shift override so nobody can break anything.
此外,出于安全原因,请确保您对代码设置密码并禁用轮班覆盖,这样任何人都无法破坏任何内容。
Use a DLookup based on their login name (which you can set as a tempvar on the login screen) to find their access level from the table.
使用基于他们的登录名(您可以在登录屏幕上将其设置为临时变量)的 DLookup 从表中查找他们的访问级别。
per your second part from the comments. I've never called a temp var in a dlookup, so I can only take a stab at it. You might need to google how to do it correctly if it doesn't work. It could need more or less quotation marks.
根据评论中的第二部分。我从来没有在 dlookup 中调用过临时变量,所以我只能尝试一下。如果它不起作用,您可能需要谷歌搜索如何正确执行。它可能需要更多或更少的引号。
dim strUserAccess as String
strUserAccess = DLookup("fieldname", "tablename", "[UserName]="" & TempVars("Username").value & "")
if strUserAccess = "Admin" then
...
and so on. I may have typoed the Dlookup, and I'm not 100% sure that this will even work, but it should do.
等等。我可能在 Dlookup 中打错了字,我不能 100% 确定这会起作用,但它应该会起作用。
回答by Deafdan
I'm designing something along the same lines, and I've found it's useful to insert the user permission into a TempVars()
from the results of the user login. It's only updated from the login, and not anywhere else in the database.
我正在按照相同的方式设计一些东西,我发现将用户权限插入到TempVars()
用户登录的结果中很有用。它仅从登录名更新,而不是从数据库中的任何其他地方更新。
Info on TempVar from MSDN, but there is a better description from the Access Blog
来自 MSDN 的关于 TempVar 的信息,但访问博客有更好的描述
On any button (to open form) intended to have limited access, the onclick event would evaluate the user permission variable. Thus I could have a MsgBox
if the user was not allowed to use the form.
在旨在具有有限访问权限的任何按钮(打开表单)上,onclick 事件将评估用户权限变量。因此,MsgBox
如果不允许用户使用该表单,我可以有一个。