martin2k

Home | Forums | VB6 | Games | Software | Directory | HTML | Wiki | Blog | Contact

How do I refuse access after three incorrect passwords?


 

Home

Read and add posts to the Visual Basic 6.0 Forums

Download some of my programs and OCXs

Read the Tips here

The old style forum is still available here

Contact


 

Question

From: MIKE SMITH
Date: Tuesday 13 March 2001 9:02 AM
Subject: Nested IF
Question: Good Day!!

I am just learning VB, I am doing a project that I need to set up an opening screen that requests a password and after three failed attempts will exit the program. Do you have any suggestions for me? Should I be using a nested IF? Look forward to hearing from you as this has got me totally confused and I can't find an example in any of my books.

Thanx,
Mike

Firstly, create a form that looks similar to this:

The password textbox needs to have the PasswordChar property set to '*'.

In the General/Declarations section of the code window, you will need to declare an Integer variable, which will count how many attempts the user has made.  When the user clicks on the OK button, the code will check to see if the correct password has been entered.  If not, the value of the Var1 variable will be incremented by 1.  If the user enters three incorrect passwords, a message box will appear saying that the program will terminate, which it does afterwards.

Dim Var1 As Integer


Private Sub Command1_Click()
If Text2.Text <> "hello" Then
    Select Case Var1
    Case Is <= 2
    MsgBox "The password is incorrect", vbExclamation, "Incorrect Password"
    Var1 = Var1 + 1
    Case Else
    MsgBox "You have entered the wrong password too many times, you are now locked out.  Please contact your system administrator.  The program will now terminate.", vbCritical, "Locked Out"
    Unload Me
    End Select
Else

MsgBox "(The correct password was entered)"
End If
End Sub


blog comments powered by Disqus

Archived Comments

From: Deep
Date: Saturday, March 26, 2011 at 09:43:51
Comments: Nice sir, it is a perfect program i found.
 
From: nickoafan
Date: Thursday, August 27, 2009 at 14:42:25
Comments: .. thanks for this web..
.. this is the program question in my midterm exam..
.. thank u very much..
 
From: seng phearin
Date: Thursday, June 25, 2009 at 06:27:49
Comments: Hi I am very happy to visit this site and i can get the solution , later i hope i can get more benifit from this site.
 
From: manisha
Date: Tuesday, February 5, 2008 at 21:45:33
Comments: hi -----
it site is really good
keep it up
thanks
 
From: Willyandago
Date: Friday, January 19, 2007 at 09:01:19
Comments: Hi i was having a problem but now am happy because i got the solution from this site keep it up men
thanx
 
From: Alison Walls
Date: Wednesday, June 29, 2005 at 12:13:59
Comments: Hi
I am trying to get my program to exit after 3 failed password attempts. I have tried the code above and can't get it to work. It doesn't seem to count the number of attempts. Any ideas?
Thanks, Alison
 
Reply: Did you declare the variable in the General/Declarations section?  Another way of doing it is to declare it in the Command1_Click event using the Static statement.  That way the variables value will be retained for as long as the code is running.
 
From: Phakama
Date: Saturday, August 28, 2004 at 13:41:45
Comments: Hi you have help me very much with your suggestions now i know how to use PassWordChar.Thank you very much.
 
Reply:

PasswordChar can be set at design time or runtime.  To set it at runtime, use the following code:

Text1.PasswordChar = "*"

To turn the PasswordChar off, set it to a zero-length string.

 
From: Lenny
Date: Thursday, July 22, 2004 at 22:51:21
Comments:

You could store the password in a textfile:

Private Sub cmdLogin_Click()
Dim strREAD As String
Dim
strPassword As String

If txtPassword.Text = "" Then
txtPassword.SetFocus

Else

Open "C:\Password.TXT" For Input As #1
Do While Not EOF(1)
    Input #1, strPassword
    strREAD = strPassword
Loop
Close
#1

If txtPassword.Text = strREAD Then
    MsgBox "Correct Password!", , ""
Else
    MsgBox "Incorrect Password!", , ""
   
End If
End If
End Sub


Private Sub Form_Load()
txtPassword.PasswordChar = "*"
End Sub


*******************************************************
You can add a registry, but you will need to add to the regisrty - "100598.reg"

Private Sub cmdLogin_Click()
Dim strREAD As String

If txtPassword.Text = "" Then
txtPassword.SetFocus

Else
strREAD = GetSetting("Project1", "STARTUP", "PASSWORD", &H8000000F)

If txtPassword.Text = strREAD Then
    MsgBox "Correct Password!", , ""
Else
    MsgBox "Incorrect Password!", , ""
   
End If
End If
End Sub


Private Sub Form_Load()
txtPassword.PasswordChar = "*"
End Sub


Yup. I think that's right! Hope this helps, geraldine!

 
From: gina
Date: Thursday, April 17, 2003 at 12:37:09
Comments: how do the same for entering username on three attempts
 
Reply:

Replace the line:

If Text2.Text <> "hello" Then

with:

If Text1.Text <> "gina" Then '"gina" is a sample username and Text1 is the username TextBox
 
From: grean
Date: Monday, February 3, 2003 at 19:39:55
Comments: what coding would you put in there if you wanted the program to switch to another form and close the login box, only if the password is correct?
thanks grean.
 
Reply:

Replace the line:

MsgBox "(The correct password was entered)"

with:

Unload Me 'Unload the login form
Form2.Show 'Replace Form2 with whatever form you want to show
 
From: Thursday, January 2, 2003 at 11:34:06
Date: Chellie
Comments: what are the codings in making a password for a system program?
 
From: Daniel
Date: Wednesday, December 4, 2002 at 11:20:58
Comments: Hello. I am having trouble in disabling a button. What it is, the program asks the user to input a password a few times. On failing to input the correct password on the attempts given, rather than closing the program, the button the user clicked is disabled. Can you please help. Bye.
 
Reply:

Just replace the line:

Unload Me

with:

Command1.Enabled = False

This will disable the button. 

 
From: geraldine
Date: Thursday, November 28, 2002 at 07:39:35
Comments: how to set the password before writing this program?
 
Reply:

The above program is just an example of how to stop the user from entering the program if they input three incorrect passwords.  The password is set in the third line:

If Text2.Text <> "hello" Then

Where the password is 'hello'.  In reality, passwords can be hard-coded in the executable or anywhere else e.g. an external file.


Martin Allen 1999 - 2012.  Last updated Friday 05 October 2012 06:28:38 PM +0100.