我目前正在一个项目上工作,其中一个要求是使用用户的Windows登录作为他们的登录MS Access,然后他们会在那里点击角色以获得进入系统。我从来没有这样做过,但我已经在Access中建立了一个登录屏幕,它从表格中提取数据。我有成功拉动用户Windows登录的代码,但在此之后我遇到了麻烦。表名是tblUser,用户是普通用户,人力资源和管理员。目前,在该表中我有指定的号码与一般用户= 1,HR = 2的角色,管理员= 3MS Access中使用的基于角色的访问控制
The Login Screen:
Log On
General User
HR
Admin
Code that pulls the user information:
Private Sub Form_Load()
Stop
Debug.Print Environ("UserName")
Debug.Print Environ$("ComputerName")
Dim strVar As String
Dim i As Long
For i = 1 To 255
strVar = Environ$(i)
If LenB(strVar) = 0& Then Exit For
Debug.Print strVar
Next
End Sub
下面是我为过去我的登录屏幕的代码。通过绘制所有东西,似乎它将是同一个过程,但我不确定。有什么我可以做的下面的代码?
Private Sub btnLogin_Click()
Dim rs As Recordset
Set rs = CurrentDb.OpenRecordset("tblUser", dbOpenSnapshot, dbReadOnly)
rs.FindFirst "UserName='" & Me.txtUserName & "'"
If rs.NoMatch = True Then
Me.lblWrongUser.Visible = True
Me.txtUserName.SetFocus
Exit Sub
End If
Me.lblWrongUser.Visible = False
If rs!Password <> Nz(Me.txtPassword, "") Then
Me.lblWrongPass.Visible = True
Me.txtPassword.SetFocus
Exit Sub
End If
Me.lblWrongPass.Visible = False
If rs!EmployeeType_ID = 3 Then
Dim prop As Property
On Error GoTo SetProperty
Set prop = CurrentDb.CreateProperty("AllowBypassKey", dbBoolean, False)
CurrentDb.Properties.Append prop
SetProperty:
If MsgBox("Would you like to turn on the bypass key?", vbYesNo, "Allow Bypass") = vbYes Then
CurrentDb.Properties("AllowBypassKey") = True
Else
CurrentDb.Properties("AllowBypassKey") = False
End If
End If
DoCmd.OpenForm "frmPersonal_Information"
DoCmd.Close acForm, Me.Name
End Sub
我希望这是足够的信息,我正在努力完成。如果需要更多信息,请告诉我。谢谢。
未拆分访问的登录屏幕是一个笑话。无论如何,像所有的会员/角色模型一样,您需要拥有用户表,角色表,user_vs_roles表。最后,登录,注销,分配角色,根据登录用户读取/验证用户角色的方法等。 –