简体   繁体   English

访问 VBA 登录应用程序

[英]Access VBA login application

I am writing this little login validation application as a step of my VBA learning process.我正在编写这个小型登录验证应用程序,作为 VBA 学习过程的一个步骤。 The application should check whether the login name is "sa" and the password is "XEL".应用程序应检查登录名是否为“sa”,密码是否为“XEL”。 The application should allow the user to enter the username and password for a maximum of three times.应用程序应允许用户最多输入 3 次用户名和密码。 If in the all of the three attempts, a wrong usename and password is entered, the application should display an error and close.如果在所有三个尝试中,都输入了错误的用户名和密码,则应用程序应显示错误并关闭。 And blow is my code:打击是我的代码:

Private Sub Command6_Click()
Me.Text0.SetFocus
cID = Text0.Text
Me.Text4.SetFocus
cPswd = Text4.Text

For i = 1 To 3
validpswd = False
If cID = "sa" And cPswd = "XEL" Then
validpswd = True
MsgBox "Successful"
Exit For
Else
MsgBox "Try again"
Exit For
End If
Next i
'MsgBox ("byebye")
'DoCmd.Close acForm, "Used Car Sales", acSaveYes

End Sub

Now it does the checking/validating part.现在它执行检查/验证部分。 But it doesn't do the three times max limitation, I am kind of confused as how to put the last message box code and close command into the whole code blocks.但它没有达到三倍最大限制,我对如何将最后一个消息框代码和关闭命令放入整个代码块感到困惑。 Any helps are appreciated.任何帮助表示赞赏。

Thank you very much.非常感谢。

What I see wrong is you are having them type in the username/password then click a button.我认为错误的是您让他们输入用户名/密码,然后单击按钮。 That button then checks to see if the username/password matches the predefined values three times in a row.然后该按钮会检查用户名/密码是否连续三次与预定义值匹配。 That is a problem because the user wouldn't be able to actually try three times.这是一个问题,因为用户实际上无法尝试 3 次。

To fix it, you create a Module/Form level variable and initialize it in the form load to 0. then you check in the button click if that variable is greater than 2. If it is, then you run your close procedure.要修复它,您创建一个模块/表单级别变量并在表单加载中将其初始化为 0。然后您检查按钮单击是否该变量大于 2。如果是,则运行您的关闭过程。 If it isnt, then it checks your username and password to see if they are correct, if they aren't then it increments the Module level variable by 1. Example below.如果不是,那么它会检查您的用户名和密码以查看它们是否正确,如果它们不正确,那么它将模块级别变量增加 1。下面的示例。

EDIT: added a check since it doesn't quite catch it after the 3rd try.编辑:添加了一个检查,因为它在第三次尝试后并没有完全抓住它。 Feels a bit ugly, but it works.感觉有点难看,但它有效。

EDIT2: Dyslexia moment, fixed the greater then, less then signs for the if statements and the associated values EDIT2:阅读障碍时刻,修复了 if 语句和相关值的较大 then 符号

Dim Tries as Long

Private Sub Form_Load()
Tries = 0

End Sub

Private Sub Command6_Click()
If Tries < 3 Then
    cID = Text0.Text
    cPswd = Text4.Text
    validpswd = False
    If cID = "sa" And cPswd = "XEL" Then
        validpswd = True
        MsgBox "Successful"
    Else
        Tries = Tries + 1
        If Tries > 2 Then
            MsgBox ("byebye")
            DoCmd.Close acForm, "Used Car Sales", acSaveYes
        else
        MsgBox "Try again"
        end if
    End If
    'MsgBox ("byebye")
    'DoCmd.Close acForm, "Used Car Sales", acSaveYes
End If
End Sub

Thanks all.谢谢大家。 This is what I did eventually.这就是我最终所做的。 I am sure there is other better way to do this and please comment to let me know as well我相信还有其他更好的方法可以做到这一点,请发表评论让我知道

If try < 3 Then


If cID <> "sa" Or cPswd <> "XEL" Then
try = try + 1
MsgBox "Try again"
Else
MsgBox "Successful"
try = 0
End If

If try = 3 Then
MsgBox ("Too many tries")
DoCmd.Close acForm, "Login Test", acSaveYes
End If

End If

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM