Error Checking in Excel
Thanks Thanks:  0
Likes Likes:  0
Results 1 to 10 of 10

Thread: What is wrong with this code

  1. #1
    New Member
    Join Date
    Apr 2002
    Location
    East Texas
    Posts
    38
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    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

  2. #2
    MrExcel MVP Al Chara's Avatar
    Join Date
    Feb 2002
    Location
    Newark, Delaware
    Posts
    1,701
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    The following line should read myTries not iTries:
    Do Until iTries = 3
    Best regards,
    Allan Chara
    http://www.mrspreadsheets.com

  3. #3
    New Member
    Join Date
    Apr 2002
    Location
    East Texas
    Posts
    38
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    That was a typo during the post. The variable does read myTries.
    Thanks in advance

  4. #4
    MrExcel MVP Al Chara's Avatar
    Join Date
    Feb 2002
    Location
    Newark, Delaware
    Posts
    1,701
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    myTries =myiTries + 1 should read:
    myTries =myTries + 1

    Is this just a typo too? Please post your code without typos.

    Best regards,
    Allan Chara
    http://www.mrspreadsheets.com

  5. #5
    MrExcel MVP Al Chara's Avatar
    Join Date
    Feb 2002
    Location
    Newark, Delaware
    Posts
    1,701
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    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)
    Best regards,
    Allan Chara
    http://www.mrspreadsheets.com

  6. #6
    New Member
    Join Date
    Apr 2002
    Location
    East Texas
    Posts
    38
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    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

  7. #7
    MrExcel MVP Al Chara's Avatar
    Join Date
    Feb 2002
    Location
    Newark, Delaware
    Posts
    1,701
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    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.
    Best regards,
    Allan Chara
    http://www.mrspreadsheets.com

  8. #8
    New Member
    Join Date
    Apr 2002
    Location
    East Texas
    Posts
    38
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    Thanks Al
    skyhook@flash.net

  9. #9
    New Member
    Join Date
    Apr 2002
    Location
    East Texas
    Posts
    38
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    Sorry Al I just changed providers the other day my new e-mail is
    skyhook@risecom.net

  10. #10
    New Member
    Join Date
    Apr 2002
    Location
    East Texas
    Posts
    38
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    Thanks Al for all the help the code worked great.

Some videos you may like

User Tag List

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •