Hey guys,

Just trying different little programs.

I was looking though a book on Visual Basic .Net and came across a little password-protection program (A very simple one).

Problem is, that this was written for VB6, not the 2008 version, so i did a little editing as shown below:

'Start by declaring variables
        Dim Password As String
        Dim Attempt As Integer
        Dim Inputpassword As String

        'Now move on to defining what the variables contain
        Password = "secret"
        Attempt = 0

        'Now tell the program what to do
        Do
            Attempt = Attempt + 1
            Inputpassword = InputBox("Enter password.  This program will close after 3 incorrect attempts.  This is attempt " & "Number " & Attempt)

            'Tell the program when it is to end
            If Attempt = 3 Then Me.Close() Else 
        Loop Until (Inputpassword = Password)

The only thing i am having difficulty is with the line at the end. Even though it has been told it needs to close if more than three attempts are made, it keeps trying to create a fourth attempt and is fighting against the If statement.

How do i get the inputbox to close without it conflicting with the loop? I thought maybe an End If statement at the end would help, but it doesn't.

Regards

Myk

Consider closing the program after the loop.

Do
            Attempt += 1
            Inputpassword = InputBox("Enter password. This program will close after 3 incorrect attempts.  This is attempt number " & Attempt)
        Loop Until (Inputpassword = Password Or Attempt = 3)

        If (Inputpassword <> Password) Then
            Me.Close()
        End If

Nah, thats not working, now it's going straight past the 3-attempt limit ...

What if i said "If inputpassword = password then me.close()" and THEN the loop?

Edit - Tried that

If Inputpassword = Password Then
                Me.Close()
            Else
                loop Until (Inputpassword = Password)
            End If

But it's giving me a lot of grief about End If's ...

You can't do that with loops.

And I think you should check my code again. It works. If it's not working for you, I suggest you post more of your source code to find another issue.

For reference, this is the full working source code of a form that has your original code snippet and my minor modification.

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        'Start by declaring variables
        Dim Password As String
        Dim Attempt As Integer
        Dim Inputpassword As String

        'Now move on to defining what the variables contain
        Password = "secret"
        Attempt = 0

        Do
            Attempt += 1
            Inputpassword = InputBox("Enter password. This program will close after 3 incorrect attempts.  This is attempt number " & Attempt)
        Loop Until (Inputpassword = Password Or Attempt = 3)

        If (Inputpassword <> Password) Then
            Me.Close()
        End If

    End Sub
This question has already been answered. Start a new discussion instead.