Hello, i would like to allow the user to press Retry button from msgbox only 3 times. I just don't succedd to do it, here is my code if anybody can help me?

Private Sub cmdValid_Click()

    Dim Response As Integer
    If txtPassword.Text = txtPassword.Tag Then
        MsgBox "You've passed security!", vbOKOnly + _
        vbExclamation, "Access Granted"
    Else
        Response = MsgBox("Incorrect password", vbRetryCancel _
        + vbCritical, "Access Denied")
        If Response = vbRetry Then
            txtPassword.SelStart = 0
            txtPassword.SelLength = Len(txtPassword.Text)
        Else
            End
        End If
    End If
    txtPassword.SetFocus 

End Sub

Recommended Answers

All 5 Replies

Try This :

Private Sub cmdValid_Click()
If (txtPassword.Text <> txtPassword.Tag) Then
    If (varx <> 3) Then
    If MsgBox("Password Incorrect", vbRetryCancel) = vbRetry Then varx = varx + 1
    Else
    MsgBox "Retry disabled"
    End If
Else
    MsgBox "Login Successful"
End If
End Sub

on Form_load()

Private Sub Form_Load()
varx = 0
End Sub

And at last , don't forget to declare varx at module level

so , In the module declare :

Public varx as Integer

hope this helps you.

@alina
DownVotes are not the right way to explain your problem .
If you have any problem then reply should be made in the form of new post. right ?.

DownVotes affects the reputation here.It doesn't mean that you should not give downvotes . you can give downvotes whenever it is required.But its not fair to you explain the problem using downvotes. right ?

its not helping me, cause after i retry wrong password, then it keeps letting me to retry how much i want

No, if you followed the instruction then it woun't let you enter the wrong password for more than three times.
I think you are getting problems due to not defining the varx at module level.

And if , you don't want to allow a user to login if the user enters the wrong information for more than 3 times then put this in code window:

    Dim varx As Integer
    Private Sub cmdValid_Click()
    If (txtpassword.Text <> txtpassword.Tag) Then
    If (varx <> 3) Then
    If MsgBox("Password Incorrect", vbRetryCancel) = vbRetry Then varx = varx + 1
    Else
    cmdvalid.Enabled = False
    cmdvalid.Caption = "Not Allowed"
    End If
    Else
    MsgBox "Login Successful"
    End If
    End Sub

    Private Sub Form_Load()
    varx = 0
    End Sub

Now you don't have to declare varx at module level because we are declaring varx atGeneral Decalaration Level

If still facing some problem Then don't forget to explain your problem clearly.

At the top of line below puclic class form1:
Dim retry As Integer

Private Sub cmdValid_Click()

    If (txtpassword.Text = txtpassword.Tag) Then
        message.show("Login Access")
    Else
        If (retry >= 3) Then
           If MsgBox("Password Incorrect", vbRetryCancel) = vbRetry Then 
           retry = retry + 1
        Else
           cmdvalid.Enabled = False
           cmdvalid.Caption = "Acess Denied"
        End If
    End If

Private Sub Form_Load()
retry = 0

More Information:
Here I declare the the retry as integer (you can set your own)
but at the top so that I don't need to type Dim retry as integer again...

you can do like this too:
cmdvalid_Click()

dim i as integer
if (txtpass.text = txtpass.tag) then
   message.show("correc pass")
Else
   if retry >= 3 then
      message.show("incorrect pass" & vbnewline & "No more retry. Sorry")
      cmdvalid.enabled = false
      'main as this will not let the user to click as we disabled valid button
   Else
      retry = retry + 1
      Message.show("Type the pass again." & vbnewline & "trial: " & retry &"/3")
   End if
End if

Form1_Load()

retry = 0
me.refresh()

Thank You

thank you guys (rishif2 and Deep Modi)
both have gaved me what i needed, and sorry Risif for downvote your comment, its my mistake, i thought that you'll not be able to see what i write, that it did not helped me your code, my bad once again. And thanks again

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.