MS Access-登录

时间:2018-07-19 15:21:08

标签: ms-access login username

当用户登录后,我希望它能够在主菜单的“标签”中显示其用户名。这是我的代码。此刻允许登录表单检查输入的详细信息,并将其与employee表进行比较以允许访问Home表单。

Option Compare Database
Option Explicit
Private Sub ButtonLogin_Click()
Dim rs As Recordset

Set rs = CurrentDb.OpenRecordset("Employees", dbOpenSnapshot, dbReadOnly)

rs.FindFirst "UserName='" & Me.TxtUsername & "'"

If rs.NoMatch 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

TempVars("EmployeeType") = rs!EmployeeType_ID.Value


DoCmd.OpenForm "Home"
DoCmd.Close acForm, Me.Name


End Sub

在此阶段之后,我被困在显示“ home”表单以显示刚刚登录(标签)的用户的代码上,这将是“未绑定”字段。

enter image description here

2 个答案:

答案 0 :(得分:0)

在这种情况下,我假设您的文本框名为Welcome。您可能需要使用以下内容:

Set rs = CurrentDb.OpenRecordset("Employees", dbOpenSnapshot, dbReadOnly)

rs.FindFirst "UserName='" & Me.TxtUsername & "'"

If rs.NoMatch Then
    Me.LblWronguser.Visible = True
    Me.TxtUsername.SetFocus
    Exit Sub
Else 
    Me.Welcome = rs!UserName
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

TempVars("EmployeeType") = rs!EmployeeType_ID.Value

DoCmd.OpenForm "Home"
DoCmd.Close acForm, Me.Name

请注意,这只会将用户名应用于Welcome文本框。如果需要名称,则需要以不同的方式引用此列,例如Me.Welcome = rs!FirstName或其他名称。另外,由于有密码验证,因此您可能想要重新组织内容,以便该名称仅在 密码部分正确传递后填充。

答案 1 :(得分:0)

假设表结构与下面类似,并且标签实际上是文本框,则可以使用Dlookup函数。

enter image description here


enter image description here