我建议为此任务使用简单的DLookup 表达式,例如,假设用户输入的密码存储在变量pwd 中,您可以使用:
DLookup("Password","Crewlist","Admin = True and Password = '" & pwd & "'")
如果没有找到匹配项,DLookup 函数将返回 Null,您可以使用 If 语句和 IsNull 函数进行测试,例如:
If IsNull(DLookup("Password","Crewlist","Admin = True and Password = '" & pwd & "'")) Then
MsgBox "Invalid Password!"
Else
' Do Stuff
End If
这里,我只指定Password字段作为要查找的字段,因为DLookup需要一个特定的字段,其值应该被返回。您可以改为使用 DCount 函数并测试返回值是否非零,例如:
If DCount("*","Crewlist","Admin = True and Password = '" & pwd & "'") > 0 Then
' Do Stuff
Else
MsgBox "Invalid Password!"
End If
在按钮的事件处理程序中实现,可以写成:
Private Sub Command0_Click()
Dim pwd As String
pwd = InputBox("Enter Password:", "Password")
If pwd <> vbNullString Then
If IsNull(DLookup("Password", "Crewlist", "Admin = True and Password = '" & pwd & "'")) Then
MsgBox "Invalid Password!"
Else
MsgBox "Access Granted!"
End If
End If
End Sub
请注意,这只是检查密码,因此,仅使用上述代码,用户可以为 任何 Admin 用户指定密码并被授予访问权限。
您可以通过附加提示轻松检查用户名:
Private Sub Command0_Click()
Dim usr As String
Dim pwd As String
usr = InputBox("Enter Username:", "Username")
If usr <> vbNullString Then
pwd = InputBox("Enter Password:", "Password")
If pwd <> vbNullString Then
If IsNull(DLookup("Password", "Crewlist", "Admin = True and Username = '" & usr & "' and Password = '" & pwd & "'")) Then
MsgBox "Invalid Username or Password!"
Else
MsgBox "Access Granted!"
End If
End If
End If
End Sub
但是,如果您要设计自己的模式表单,其中包含用于用户名的文本框或组合框以及用户可以在其中指定密码的文本框,这将更加专业。
除此之外,以纯文本形式将密码存储在数据库中是不好的做法:考虑使用适当的 hash function 对密码进行哈希处理并存储哈希值。然后,将相同的散列函数应用于用户输入,并使用生成的散列值来测试数据库中的匹配项。
这样,只有用户知道密码——因为散列是一个单向过程,即使是数据库管理员也不知道用户的密码。如果用户需要更改他们的密码,他们将获得一个可以更改的新临时密码,或者在其他一些身份验证之后提供一个新密码。
作为一般规则,永远不要相信任何能够向您发送原始密码的服务 - 这表明此类服务正在存储密码而不进行加密/屏蔽。