Dim i As Integer
If Me.txt姓名.Text.Length < 1 Or Me.txt密码.Text.Length < 1 Then
MsgBox("用户名或密码不能为空!", MsgBoxStyle.Critical, "出错提示")
ElseIf Me.txt姓名.Text.Length >= 1 Then
If Me.txt密码.Text <> 123 Then
If i < 2 Then
i = i + 1
Me.txt密码 = ""
MsgBox("密码错误!", MsgBoxStyle.Critical, "警告")
ElseIf i = 2 Then
MsgBox("你已经3次输错密码,系统将退出!", MsgBoxStyle.Critical, "警告")
Exit Sub
End If
ElseIf Me.txt密码.Text = 123 Then
MsgBox("欢迎" & Me.txt姓名.Text, , "欢迎")
End If
End If
为什么没有3次错误的退出提示,请各位看看错在哪里,谢啦!
If Me.txt姓名.Text.Length < 1 Or Me.txt密码.Text.Length < 1 Then
MsgBox("用户名或密码不能为空!", MsgBoxStyle.Critical, "出错提示")
ElseIf Me.txt姓名.Text.Length >= 1 Then
If Me.txt密码.Text <> 123 Then
If i < 2 Then
i = i + 1
Me.txt密码 = ""
MsgBox("密码错误!", MsgBoxStyle.Critical, "警告")
ElseIf i = 2 Then
MsgBox("你已经3次输错密码,系统将退出!", MsgBoxStyle.Critical, "警告")
Exit Sub
End If
ElseIf Me.txt密码.Text = 123 Then
MsgBox("欢迎" & Me.txt姓名.Text, , "欢迎")
End If
End If
为什么没有3次错误的退出提示,请各位看看错在哪里,谢啦!
改成:Static i As Integer
或者把那个变量 i 用 Private 声明成模块(窗体)级的变量。另外,你的语法不严谨:
你的那3句 ElseIf ………… Then
都应该为 Else还有一个就是句法问题,你的 MsgBox语句中的括号都应该去除。
你的代码一开始就是Dim i As Integer
相当于i每次都是重新投胎,从0开始, 上一辈子的事情当然忘光光咯, 也就不记得什么+1+2的事了.还有,这代码的逻辑挺乱.现在闲着就帮你写个流程:
1: 定义一个窗体级变量I计数
2: 开始登录(写在按钮的事件里或者其他什么窗体过程中)
如果用户名密码正确,那么: 加载下一个窗体, 卸载登录窗体
否则: I加1, 并且判断I是否等于3, 如果等于3那么退出整个程序(没有否则)就这么些.
至于判断用户名密码,不要一个一个判断, 层层IF嵌套无端增加逻辑复杂度, 直接用一个if判断用户名和密码是否相符即可, 多个条件用and或or之类的连接.
Private Sub Command1_Click()
If Text1.Text = "123" Then
MsgBox "对"
Else
i = i + 1
MsgBox "错"
If i > 3 Then
MsgBox "你已经输入错误三次即将退出"
End
End If
End If
End SubPrivate Sub Form_Load()
i = 1
End Sub
If Me.txt姓名.Text.Length < 1 Or Me.txt密码.Text.Length < 1 Then
MsgBox("用户名或密码不能为空!", MsgBoxStyle.Critical, "出错提示")
ElseIf Me.txt姓名.Text.Length >= 1 Then
If Me.txt密码.Text <> 123 Then
If i < 2 Then
i = i + 1
Me.txt密码 = ""
MsgBox("密码错误!", MsgBoxStyle.Critical, "警告")
ElseIf i = 2 Then
MsgBox("你已经3次输错密码,系统将退出!", MsgBoxStyle.Critical, "警告")
end'退出程序
Exit Sub
End If
ElseIf Me.txt密码.Text = 123 Then
MsgBox("欢迎" & Me.txt姓名.Text, , "欢迎")
End If
End If
后面n = n + 1
If n >= 5 Then
MsgBox "对不起,输入错误次数超过5次,限定您使用本系统,请确认正确用户名和密码后再试!", , "错误提示"
Unload Me
Static a As Integer
a = a + 1
Label2.Visible = True
If Text1.Text = b Then
Label2.Caption = "点击请进入留言板"
Label2.ForeColor = vbBlue
Else
If a < 3 Then
Label2.Caption = "密码错误!"
Label2.ForeColor = vbRed
Text1.SelStart = 0
Text1.SelLength = Len(Text1.Text)
Text1.SetFocus
Else
Label2.Caption = "3次都未通过密码检测,判定为非法用户。请退出"
Label2.ForeColor = vbRed
Command2.Visible = True
Command1.Enabled = False
Text1.Text = ""
Text1.Locked = True
End If
End If
我自己曾经编写的一小段编码,里面有你要的
if 条件
如果达成条件,就执行这里的语句,执行完后,直接跳到end [你的问题在这里,你认为执行完这里,会继续执行then后面的语句,这里要理解一下,执行完就跳到end了,不会运行then或thenif后面的所有语句,是直接跳到对应这个if的end了then
否则执行这里的语句,执行完后到end
end还有是全局变量和局部变量的问题,
如果你这个是是函数的话,i定义了局部变量,那么i每次运行的初始值是0