首页 > 解决方案 > MS Access - 登录

问题描述

当用户登录时,我希望他们的用户名显示在主菜单的标签中。这是我的代码。此时允许登录表单检查输入的详细信息并将它们与员工表进行比较以允许访问主页表单。

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

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

在此处输入图像描述

标签: ms-accesslogin

解决方案


对于这种情况,我将假设您的文本框被称为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或其他内容。此外,由于存在密码验证,您可能需要重新构建内容,以便仅在正确传递密码部分才会填充名称。


推荐阅读