将标签的标题设置为用户名

Set the Caption of label to Username

我正在制作一个 Access 数据库并具有安全登录功能。我想将在标签或其他控件中输入的用户名用作“登录为”功能。

如何引用不是我的登录表单的其他表单?

我想将 ID 或用户名附加到表单 UserViewForm 中的标签 66。

Private Sub Command1_Click()

Dim User As String
Dim UserLevel As Integer
Dim TempPass As String
Dim ID As Integer
Dim UserName As String
Dim TempID As String

If IsNull(Me.TxtUserName) Then
 MsgBox "Please enter UserName", vbInformation, "Username required"
 Me.TxtUserName.SetFocus
ElseIf IsNull(Me.TxtPassword) Then
 MsgBox "Please enter Password", vbInformation, "Password required"
 Me.TxtPassword.SetFocus
Else
 If (IsNull(DLookup("UserLogin", "tblUser", "UserLogin = '" & Me.TxtUserName.Value & "' And UserPassword = '" & Me.TxtPassword.Value & "'"))) Then
 MsgBox "Invalid Username or Password!"
 Else
 TempID = Me.TxtUserName.Value
 UserName = DLookup("[UserName]", "tblUser", "[UserLogin] = '" & Me.TxtUserName.Value & "'")
 UserLevel = DLookup("[UserType]", "tblUser", "[UserLogin] = '" & Me.TxtUserName.Value & "'")
 TempPass = DLookup("[UserPassword]", "tblUser", "[UserLogin] = '" & Me.TxtUserName.Value & "'")
 UserLogin = DLookup("[UserLogin]", "tblUser", "[UserLogin] = '" & Me.TxtUserName.Value & "'")
 DoCmd.Close
 If (TempPass = "password") Then
 MsgBox "Please change Password", vbInformation, "New password required"
 DoCmd.OpenForm "frmUserinfo", , , "[UserLogin] = " & UserLogin
 Else
 'open different form according to user level
 If UserLevel = 1 Then ' for admin
 DoCmd.OpenForm "UserViewForm"
 'Else
 'DoCmd.OpenForm "Ne"
 End If
 End If
 End If
End If

Forms!UserViewFrom.Label66 = TempID

End Sub

这是为了用户体验。

对于标签,您需要设置 Caption 属性。

Forms!UserViewFrom.Label66.Caption = TempID

另请注意,您不应以明文形式存储密码,也不应使用未经处理的用户输入来构建 SQL 命令 - 查找 SQL 注入。