Hi i'm making a password program for my programming class, and im done but the second part is to have an error/msgbox come up after 5 tries, i don't know how to do it, i've attempted to but i've failed, this is what i have right now
*********************************************************************************************************
Private Sub cmdOK_Click()
Const secretPassword As Integer = 1234
Dim guess As Integer
guess = Val(txtPassword.Text)
If guess = secretPassword Then
MsgBox ("Password Accepted")
Else
txtCheck = ("Incorrect Password-Please try again!")
End If
If txtPassword.Text = "1234" Then
Form2.Show
End If
End Sub
Copyright © 2024 QUIZLS.COM - All rights reserved.
Answers & Comments
Verified answer
You have to create a static variable or a form level variable that will keep track of the number of attempts.
Private Sub cmdOK_Click()
Const secretPassword As Integer = 1234
int NumberOfGuesses = 0;
Dim guess As Integer
guess = Val(txtPassword.Text)
If guess = secretPassword Then
MsgBox ("Password Accepted")
Else
txtCheck = ("Incorrect Password-Please try again!")
NumberOfGuesses ++;
End If
If txtPassword.Text = "1234" Then
Form2.Show
End If
If NumberOfGuesses == 5
MessageBox.Show(You've guessed wrong 5 times);
End Sub
I don't have my compiler with me so you'll have to compile this. You may have to add () somewhere if I forgot. I'm going off top of my head. Also I usually program C++ so I hope this works in visual basic.