Password - 3 chances and **off

S

Shake

I have put together a macro for entering a password. How do I go about
putting in a loop or some other ingenious bit of code so that you only
get 3 chances before you get booted out.

My code is attached to a very simple form and is as follows:

Public Sub PasswordOK_Click()
Dim Password As String
Dim count As Integer
Password = ""
If TextBox1 = Password Then
Personal_Details.Show
Unload Me
End If

If TextBox1 <> Password Then
TextBox1 = ""
Label1.Visible = True
PasswordOK.Visible = False
PasswordCancel.Visible = False
Application.Wait Now + TimeValue("00:00:02")
Label1.Visible = False
PasswordOK.Visible = True
PasswordCancel.Visible = True
End If
End Sub
Private Sub PasswordCancel_Click()
Unload Me
End Sub
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As
Integer)
If CloseMode = vbFormControlMenu Then
Cancel = True
Beep
End If
End Sub
 
T

Tushar Mehta

Before you get to the 3 chances bit, you have some more basic problems
to address.

Look at the code
Password = ""
If TextBox1 = Password Then

Unless TextBox1 is empty your test will always fail since the expected
password is the zero length string.

Also, and depending on what you mean to do
Personal_Details.Show
Unload Me

may not really be what you want. Instead, it might be more appropriate
to use
me.hide
personal_details.show

--
Regards,

Tushar Mehta
www.tushar-mehta.com
Excel, PowerPoint, and VBA add-ins, tutorials
Custom MS Office productivity solutions
 
S

Shake

I have put this in as an example as i did not want to show my password.

Password = ""
If TextBox1 = Password Then

However we can assume that the code will read something like

Password = "Bob"
If TextBox1 = Password Then.

I chose unload me so that if the code was eneted incorrectly or the
cancel button is hit then the form does not load. Therefore the address
book can not be accessed.

Is there a bit of code for a three chances and then exit the form.
 
D

davidm

Try below. However, if Excel is exited and re-opened, the user gets a
new lease of lease for another 3 attempts.


Public Sub PasswordOK_Click()
Dim Password As String
Dim count As Integer
STATIC CNT


CNT = CNT+1
IF CNT =3 THEN EXIT SUB

Password = "*SESAME*"
If TextBox1 = Password Then
Personal_Details.Show
Unload Me
End If

If TextBox1 <> Password Then
TextBox1 = ""
Label1.Visible = True
PasswordOK.Visible = False
PasswordCancel.Visible = False
Application.Wait Now + TimeValue("00:00:02")
Label1.Visible = False
PasswordOK.Visible = True
PasswordCancel.Visible = True
End If
End Sub
 
D

davidm

Try below. However, if Excel is exited and re-opened, the user gets
new lease of life for another 3 attempts.


Public Sub PasswordOK_Click()
Dim Password As String
Dim count As Integer
STATIC CN


CNT = CNT+1
IF CNT =3 THEN EXIT SU

Password = "*SESAME*"
If TextBox1 = Password Then
Personal_Details.Show
Unload Me
End If

If TextBox1 <> Password Then
TextBox1 = ""
Label1.Visible = True
PasswordOK.Visible = False
PasswordCancel.Visible = False
Application.Wait Now + TimeValue("00:00:02")
Label1.Visible = False
PasswordOK.Visible = True
PasswordCancel.Visible = True
End If
End Su
 
D

davidm

Shake,

My pleasure. I was just having another look at the code. There is
small howler (which you probably picked up).

If cnt *=*3 Then Exit sub should be If cnt* >*3 Then Exit sub.


Davi
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top