What is wrong with this code

Skyhook

New Member
Joined
Apr 8, 2002
Messages
38
Can someone tell me why this code will not give me three tries
at entering the password. It will only give me one try and then will close.

Private Sub CommandButton1_Click()
Dim myTries As Integer
Dim strPass As String

strPass = TextBox1.Text

Do Until iTries = 3
If strPass = "password" Then
MsgBox "Password Accepted"
Sheets("Main").Visible = True
Unload Me
End
End If

MsgBox "Incorrect Password"
myTries =myiTries + 1
TextBox1.SetFocus
Exit Do
Loop

If myTries = 3 Then Unload Me
Application.DisplayAlerts = False
ThisWorkbook.Close

End Sub

Thanks in advance
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
myTries =myiTries + 1 should read:
myTries =myTries + 1

Is this just a typo too? Please post your code without typos.
 
Upvote 0
Actually your code will not work as it is. You set your strstrPass = TextBox1.Text
How is the user supposed to change the text in the textbox if the macro is running. Everytime you hit the commandbutton mytries looses its scope and returns to 0. You have to declare it as a public variable and throw it in a module. You code needs a lot of help. Post with what you want to do, and I will write something for you (after lunch)
 
Upvote 0
Al, there is no hurry on this. It is just something I am using to try and teach myself (with the boards help) a little bit of VBA. So what I have is a variable in the workbook open event (which doesn’t work). I am trying to have something that will look at the current date and if this date is later than my Endate then it will load userform1, which is where the password has to be entered. The following is my code so far. Thanks a million for the help.

Private Sub Workbook_Open()

Dim EndDate As Date
EndDate = Date

If EndDate >= "4-20-02" Then UserForm1.Show

Sheets("Sheet1").xlVeryHidden
Sheets("Main").xlVeryHidden

End Sub


Private Sub CommandButton1_Click()
Dim myTries As Integer
Dim strPass As String

strPass = TextBox1.Text

Do Until myTries = 3
If strPass = "password" Then
MsgBox "Password Accepted"
Sheets("Main").Visible = True
Unload Me
End
End If

MsgBox "Incorrect Password"
myTries = myTries + 1
TextBox1.SetFocus
Exit Do
Loop

If myTries = 3 Then Unload Me
Application.DisplayAlerts = False
ThisWorkbook.Close

End Sub
 
Upvote 0
I created a nice little workbook that gives a good password verification userform/macro that should do what you want. Repy with your email and I will send it to you.
 
Upvote 0

Forum statistics

Threads
1,213,543
Messages
6,114,240
Members
448,555
Latest member
RobertJones1986

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top