Results 1 to 15 of 15

Thread: password, 3 times etc

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2002
    Location
    n.ireland, UK
    Posts
    157

    password, 3 times etc

    How would I edit this so if a password was entered wrong three times it'll exit the program?

    Thanks for any help.

    VB Code:
    1. Private Sub cmdLogin_Click()
    2.     Dim PassRS As Recordset
    3.     Set PassRS = Db.OpenRecordset("Login")
    4.     While Not PassRS.EOF And Not LoginOK
    5.         If (PassRS.Fields("password").Value = txtPassword.Text) And (PassRS.Fields("Username").Value = cboLogin.Text) Then
    6.             LoginOK = True
    7.             CurrentUser = PassRS("Username").Value
    8.         Else
    9.             PassRS.MoveNext
    10.         End If
    11.     Wend
    12.  
    13.     If LoginOK Then
    14.         Beep
    15.         frmIntro.Show
    16.         frmPassword.Hide
    17.         txtPassword = ""
    18.         LoginOK = False
    19.     Else
    20.         Response = MsgBox("Incorrect password", vbRetryCancel + vbCritical, "Access Denied")
    21.         If Response = vbRetry Then
    22.             txtPassword.SetFocus
    23.             txtPassword.SelStart = 0
    24.             txtPassword.SelLength = Len(txtPassword.Text)
    25.         End If
    26.     End If
    27. End Sub

  2. #2
    Frenzied Member Memnoch1207's Avatar
    Join Date
    Feb 2002
    Location
    DUH, Guess...Hint: It's really hot!
    Posts
    1,861
    add an integer variable to be a counter
    Code:
    Private Sub cmdLogin_Click()
        Dim Count As Integer <----------
        Dim PassRS As Recordset
        Count = 0 <------Initialize it to 0
        Set PassRS = Db.OpenRecordset("Login")
        While Not PassRS.EOF And Not LoginOK
            If (PassRS.Fields("password").Value = txtPassword.Text) And (PassRS.Fields("Username").Value = cboLogin.Text) Then
                LoginOK = True
                CurrentUser = PassRS("Username").Value
            Else
                PassRS.MoveNext
            End If
        Wend
    
        If LoginOK Then
            Beep
            frmIntro.Show
            frmPassword.Hide
            txtPassword = ""
            LoginOK = False
        Else
            Response = MsgBox("Incorrect password", vbRetryCancel + vbCritical, "Access Denied")
            If Response = vbRetry Then
                txtPassword.SetFocus
                txtPassword.SelStart = 0
                txtPassword.SelLength = Len(txtPassword.Text)
                Count = Count + 1 <---------Add 1 to Counter
                If(Counter > 3) Then <--------When Counter exceeds 3 show Msgbox
                   MsgBox "3 Invalid Login Attempts...This program will now Close"
                   End
                End If
            End If
        End If
    End Sub
    Being educated does not make you intelligent.

    Need a weekend getaway??? Come Visit

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jan 2002
    Location
    n.ireland, UK
    Posts
    157
    Cheers Memnoch

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Jan 2002
    Location
    n.ireland, UK
    Posts
    157
    Hm, weird count isn't going past 1, any ideas?

  5. #5
    Frenzied Member Shawn N's Avatar
    Join Date
    Dec 2001
    Location
    Houston
    Posts
    1,631
    Change

    Dim Count As Integer

    to

    Static Count As Integer
    Please rate my post.

  6. #6
    Stiletto
    Guest
    Originally posted by Shawn N
    Change

    Dim Count As Integer

    to

    Static Count As Integer
    What for?

  7. #7
    Destined Soul
    Guest
    Originally posted by neilm
    Hm, weird count isn't going past 1, any ideas?
    Um, in his code he has:
    VB Code:
    1. Count = Count + 1
    2. If(Counter > 3) Then
    3. Msgbox
    I think if you changed Counter to Count, it should work. This, of course, what you'd run into if you copied Memnoch1207's code.

    Oh, and you don't need to change to the static part, either.

    Destined

  8. #8
    Frenzied Member Shawn N's Avatar
    Join Date
    Dec 2001
    Location
    Houston
    Posts
    1,631
    ...because when you declare a variable using "Dim" the lifetime of the variable keeps its value only as long as the procedure is running. Once the function exits the memory used by the variable is zeroed out and you have nothing.

    Using "Static" will keep make sure the variable holds its data.
    Please rate my post.

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Jan 2002
    Location
    n.ireland, UK
    Posts
    157
    Yea, sorry forgot to say I changed that part destined soul.. still stickin to 1 though
    <edit> changed dim to static too, odd it should work.. </edit>

  10. #10
    Destined Soul
    Guest
    Ah, i didn't bother readin the rest of the code. Yes, it should be static, or else count will be set to 0 everytime they click the button. As well, you'll probably want to add count = 0 for when the login is done correctly (reset it so they can log in later...)

    Destined

  11. #11
    Stiletto
    Guest
    VB Code:
    1. Count[b]er[/b] = Count[b]er[/b] + 1 <---------Add 1 to Counter
    2. If(Counter [COLOR=deeppink]=[/COLOR] 3) Then 'Change this to =
    3.    MsgBox "3 Invalid Login Attempts...This program will now Close"
    4.    End
    5. End If

  12. #12

    Thread Starter
    Addicted Member
    Join Date
    Jan 2002
    Location
    n.ireland, UK
    Posts
    157
    Here's the code but updated..
    VB Code:
    1. Private Sub cmdLogin_Click()
    2.     Dim PassRS As Recordset
    3. Static PassCount As Integer
    4.     'Initialize count to 0
    5.     PassCount = 0
    6.     Set PassRS = Db.OpenRecordset("Login")
    7.     While Not PassRS.EOF And Not LoginOK
    8.         If (PassRS.Fields("password").Value = txtPassword.Text) And (PassRS.Fields("Username").Value = cboLogin.Text) Then
    9.             LoginOK = True
    10.             CurrentUser = PassRS("Username").Value
    11.         Else
    12.             PassRS.MoveNext
    13.         End If
    14.     Wend
    15.  
    16.     If LoginOK Then
    17.         Beep
    18.         frmIntro.Show
    19.         frmPassword.Hide
    20.         txtPassword = ""
    21.         LoginOK = False
    22.         mdiMain.lblLoginTime.Caption = Format$(Now, "H:MM AMPM")
    23.         mdiMain.lblUsername.Caption = CurrentUser
    24.         mdiMain.timDisplay.Enabled = True
    25.         mdiMain.mnuLogoff.Caption = "Log off " & CurrentUser
    26.     Else
    27.         Response = MsgBox("Incorrect password", vbRetryCancel + vbCritical, "Access Denied")
    28.         PassCount = PassCount + 1
    29.         MsgBox PassCount
    30.         If Response = vbRetry Then
    31.             txtPassword.SetFocus
    32.             txtPassword.SelStart = 0
    33.             txtPassword.SelLength = Len(txtPassword.Text)
    34.             'Add 1 to Counter
    35.             'PassCount = PassCount + 1
    36.             'When Counter exceeds 3 show Msgbox
    37.             If PassCount = 3 Then
    38.                 MsgBox "Three invalid login attempts... this program will now shut down", vbInformation, "Program shutdown"
    39.                 End
    40.             End If
    41.         ElseIf Response = vbCancel Then
    42.             Unload frmPassword
    43.             End
    44.         End If
    45.     End If
    46. End Sub

  13. #13
    Frenzied Member Shawn N's Avatar
    Join Date
    Dec 2001
    Location
    Houston
    Posts
    1,631

    Post

    Option Explicit takes care of those little problems.
    Please rate my post.

  14. #14

    Thread Starter
    Addicted Member
    Join Date
    Jan 2002
    Location
    n.ireland, UK
    Posts
    157
    Originally posted by Shawn N
    Option Explicit takes care of those little problems.
    It gives an error, highlighting Static and the msg says "Invalid outside procedure" when doing that..

  15. #15

    Thread Starter
    Addicted Member
    Join Date
    Jan 2002
    Location
    n.ireland, UK
    Posts
    157
    Got it thanks guys

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