Results 1 to 7 of 7

Thread: Why Doesn't this code work?

  1. #1

    Thread Starter
    Member
    Join Date
    Aug 2000
    Posts
    52
    This code is supposed to basically limit the number of attempts a user has at changing the pc password. It is then meant to invoke to unload function. Anyone have any ideas why it isnt working and it is allowing many many tries.
    Once the old pwd is correct it does what it is meant to.

    It just doesnt unload when the attempts >=3 (which is the limit i set)

    Any help is appreciated. This on has me stumpted.

    Private Sub cmdchange_Click()
    'Record how many attempts have been made.
    Static iTries As Integer

    'Check the user's password to the one that has been saved.
    If MMain.Encrypt(txtOldPassword.Text) = MMain.Password Then
    If txtNewPassword.Text = txtConfirm.Text Then
    'Save the new password.
    MMain.Password = txtNewPassword.Text

    MsgBox "Password changed.", vbInformation

    Unload Me
    Else
    iTries = iTries + 1
    If iTries >= MAX_TRIES Then
    'The user did not enter the password in the allowed number of tries.
    Unload Me
    Else
    MsgBox "Wrong password. Try again.", vbExclamation
    End If

    'Highlight the old password.
    With txtOldPassword
    .SetFocus
    .SelStart = 0
    .SelLength = Len(.Text)
    End With
    End If
    End If
    End Sub


    Any hints?

    regards
    Morpheus.

  2. #2
    Addicted Member Shrog's Avatar
    Join Date
    Aug 1999
    Location
    Darkest Africa
    Posts
    186
    Have a look at the part that I marked with "HERE IS YOUR PROBLEM". You count the number of tries after the Else statement. That part will be performed only if the new password does not match the confirm password, and NOT when the user enters an incorrect password. You might want to put an "End If" in front of that "Else".

    Code:
    Private Sub cmdchange_Click() 
      'Record how many attempts have been made. 
      Static iTries As Integer 
    
      'Check the user's password to the one that has been saved. 
      If MMain.Encrypt(txtOldPassword.Text) = MMain.Password Then 
        If txtNewPassword.Text = txtConfirm.Text Then 
          'Save the new password. 
          MMain.Password = txtNewPassword.Text 
    
          MsgBox "Password changed.", vbInformation 
    
          Unload Me 
        Else ' *** HERE IS YOUR PROBLEM *** 
          iTries = iTries + 1 
          If iTries >= MAX_TRIES Then 
            'The user did not enter the password in the allowed number of tries. 
            Unload Me 
          Else 
            MsgBox "Wrong password. Try again.", vbExclamation 
          End If 
    
          'Highlight the old password. 
          With txtOldPassword 
            .SetFocus 
            .SelStart = 0 
            .SelLength = Len(.Text) 
          End With 
        End If 
      End If 
    End Sub
    Shrog


    [Edited by Shrog on 11-29-2000 at 02:02 AM]

  3. #3
    Guest

    Counter reset?

    Do you ever reset your counter in case the user wants to change the password a second time?

  4. #4

    Thread Starter
    Member
    Join Date
    Aug 2000
    Posts
    52

    Angry

    Althouh i still can't get that damn code to work. You had me thinking for a minute when you asked about the counter.

    The application is designed to run on Win9x and initiate when the GUI kicks in, so the counter will reset itself each time the machine is restarted. It doesnt actualyl record the counts anywhere iother than the cache associated witht he live app.

    I added and end if and also looked a little more at structure (Messy) but still to no avail.

    Anyone else have any other ideas on how to do the same thing?


    regards
    M.

  5. #5
    Guest
    Check the lines with the asterisks.
    Try it out.


    Private Sub cmdchange_Click()
    'Record how many attempts have been made.
    Static iTries As Integer

    'Check the user's password to the one that has been saved.
    If MMain.Encrypt(txtOldPassword.Text) = MMain.Password Then
    If txtNewPassword.Text = txtConfirm.Text Then
    'Save the new password.
    MMain.Password = txtNewPassword.Text

    MsgBox "Password changed.", vbInformation

    Unload Me
    endif '<------Add this line ******************
    Else
    iTries = iTries + 1
    If iTries >= MAX_TRIES Then
    'The user did not enter the password in the allowed number of tries.
    Unload Me
    Else
    MsgBox "Wrong password. Try again.", vbExclamation
    End If

    'Highlight the old password.
    With txtOldPassword
    .SetFocus
    .SelStart = 0
    .SelLength = Len(.Text)
    End With
    End If
    ' End If 'Kill this line ***********************
    End Sub

  6. #6
    Addicted Member
    Join Date
    Jul 2000
    Location
    California
    Posts
    154
    Can i suggest debugging the code line by line.
    I know i've fixed a lot of problems that way.
    VB-World addict!

    All spelling errors are undocumented words!
    http://www.bells.f2s.com

  7. #7

    Thread Starter
    Member
    Join Date
    Aug 2000
    Posts
    52
    The code now works. Thanks to all who offered tips.

    Much appreciated.

    M.

Posting Permissions

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



Click Here to Expand Forum to Full Width