Ms access 如何使VBA for Access检查登录表单中未输入的记录数据?
我有一个包含UserLogin、password、Names和SecurityLvl字段的表。我还有一个表单,它有两个输入框用于输入UserLogin和password,并检查这些信息是否正确。我想知道是否有一种方法可以检查SecurityLvl是否是Admin、Student或Professor,其ID与通过UserLogin和password登录的ID相同。下面是我的登录按钮代码Ms access 如何使VBA for Access检查登录表单中未输入的记录数据?,ms-access,vba,Ms Access,Vba,我有一个包含UserLogin、password、Names和SecurityLvl字段的表。我还有一个表单,它有两个输入框用于输入UserLogin和password,并检查这些信息是否正确。我想知道是否有一种方法可以检查SecurityLvl是否是Admin、Student或Professor,其ID与通过UserLogin和password登录的ID相同。下面是我的登录按钮代码 Private Sub Command1_Click() Dim AccessLvl As String 'If
Private Sub Command1_Click()
Dim AccessLvl As String
'If there is no password or username then shows pop-up
'usertext is username box and passtext is password box
If IsNull(Me.usertext) Then
MsgBox "Please enter login", vbInformation, "LOGIN REQUIRED"
Me.usertext.SetFocus
ElseIf IsNull(Me.passtext) Then
MsgBox "Please enter password", vbInformation, "PASSWORD REQUIRED"
Me.passtext.SetFocus
'Sets actual values from table to values started above and checks if username and password actually match up with table
Else
If (IsNull(DLookup("[UserLogin]", "LoginTable", "[UserLogin] ='" & Me.usertext.Value & "' and password = '" & Me.passtext.Value & "'"))) Then
MsgBox "Username/password not valid"
Else
MsgBox "Login successful"
End If
End If
End Sub
DLookup可以查看并返回多个(连接的)字段,因此您可以尝试以下方法,只需做一些小改动:
Private Sub Command1_Click()
Dim retValue As Variant, AccessLvl As String
With Me
Select Case True
Case IsNull(.usertext.Value):
MsgBox "Please enter login", vbInformation, "LOGIN REQUIRED"
.usertext.SetFocus
Case IsNull(.passtext.Value):
MsgBox "Please enter password", vbInformation, "PASSWORD REQUIRED"
.passtext.SetFocus
Case Else:
retValue = DLookup("[UserLogin] & ',' & [SecurityLvl]", "LoginTable", "[UserLogin] ='" & .usertext.Value & _
"' and password = '" & .passtext.Value & "'")
'This will return a string in the form of "User,AccessRights"
'Just split the string where the comma is and get the second part.
'The username is the first part so it can be obtained using
'Split(retValue, ",")(0)
If IsNull(retValue) Then
MsgBox "Username/password not valid"
Exit Sub
End If
AccessLvl = Split(retValue, ",")(1)
MsgBox "Login successful"
End Select
End With
End Sub
您可以使用usertext.Value作为条件执行另一个DLookup()。