我目前正在做一个项目,其中一个要求是使用用户 Windows 登录作为他们的 MS Access 登录,然后他们将在其中单击该角色以访问系统。我以前从未这样做过,但我在 Access 中设置了一个登录屏幕,它从表中提取数据。我有成功拉取用户窗口登录的代码,但在此之后我遇到了麻烦。表名是 tblUser,用户是 General User、HR 和 Admin。目前,在表中,我分配的角色编号为 General User = 1、HR = 2、Admin = 3。
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
我希望这是我想要完成的足够信息。如果需要更多信息,请告诉我。谢谢你。