This is my first time to do visual basic program
Please help me about this problem... When I run the program with maximum attempt of 3 with correct username and password, Illegal user will pop up,,,i really dont know what to do..illegal user must be pop up when i already attempt " try again " then another "3attempt" then the username or password are wrong....pls help me

Imports System.Data.OleDb
Public Class SignIn
    Dim ctr As Integer = 0
    Dim crr As Integer = 0
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        ctr += 1
        Dim t As New Timer()
        t.Interval = 1000
        t.Enabled = True 'for timer

        Dim cmd As OleDbCommand
        Dim cmdd As OleDbCommand
        Dim cmddd As OleDbCommand
        Dim con As New OleDbConnection("Provider=Microsoft.JET.OLEDB.4.0;data source=C:\Users\Lle\Desktop\DB1.mdb")
        Dim sql = "SELECT Username,Password FROM Login WHERE Username = '" & TextBox1.Text & "' AND password = '" & TextBox2.Text & "'"
        con.Open()
        cmd = New OleDbCommand(sql, con)
        Dim sdr As OleDbDataReader = cmd.ExecuteReader()
        Dim sqll = "SELECT Username,Password FROM Login WHERE Username = '" & TextBox1.Text & "'"
        cmdd = New OleDbCommand(sqll, con)
        Dim sdrr As OleDbDataReader = cmdd.ExecuteReader()
        Dim sqlll = "SELECT Username,Password FROM Login WHERE password = '" & TextBox2.Text & "'"
        cmddd = New OleDbCommand(sqlll, con)
        Dim sdrrr As OleDbDataReader = cmddd.ExecuteReader()

        ' If the record can be queried, Pass verification and open another form.  
        If (sdr.Read() = True) Then
            Timer2.Start()
            ProgressBar1.Show()
        ElseIf (sdrr.Read() = True) Then
            MessageBox.Show("Invalid password")
            TextBox1.Text = ""
            TextBox2.Text = ""
            TextBox1.Focus()
        ElseIf (sdrrr.Read() = True) Then
            MessageBox.Show("Invalid username")
            TextBox1.Text = ""
            TextBox2.Text = ""
            TextBox1.Focus()
        Else
            MessageBox.Show("Invalid username and password!")
            TextBox1.Text = ""
            TextBox2.Text = ""
            TextBox1.Focus()
        End If


        If ctr = 3 Then
            crr += 1
            If (crr < 2) Then
                MessageBox.Show("Unidentified user", "Error Acquired", MessageBoxButtons.OK, MessageBoxIcon.Warning)
                If (MessageBox.Show("Try again?", "Error Acquired", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) = Windows.Forms.DialogResult.Cancel) Then
                    Me.Close()
                Else
                    ctr = 0
                    Label1.ForeColor = Color.Red
                    Label1.BackColor = Color.DarkGray
                    Label2.BackColor = Color.DarkGray
                    Label2.ForeColor = Color.Red
                    TextBox1.ForeColor = Color.Red
                    TextBox2.ForeColor = Color.Red

                End If
            Else
                If crr = 2 Then
                    MessageBox.Show("Illegal User!!!" + vbCr + "You are not allow to attempt again", "Error Acquired", MessageBoxButtons.OK, MessageBoxIcon.Warning)
                    Me.Close()
                End If
            End If
        End If
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        TextBox1.Text = ""
        TextBox2.Text = ""
        TextBox1.Focus()
        'button clear
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Me.Close()
        'button exit
    End Sub
    Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
        If e.KeyData = Keys.Return Then
            TextBox2.Focus()
            'when you are finish typing in textbox1 and press enter it will automatically go to textbox2
        End If
    End Sub

    Private Sub TextBox2_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox2.KeyDown
        If e.KeyData = Keys.Return Then
            Button1.Focus()
            'when you are finish typing in textbox2 and press enter it will automatically go to button1
        End If
    End Sub
    Private Sub ValidateInputs(ByVal Sender As Object, ByVal e As EventArgs)
        Button1.Enabled = Not (TextBox1.Text = String.Empty OrElse TextBox2.Text = String.Empty)
        'this is for button not to enable when textbox is empty
    End Sub

    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ProgressBar1.Hide()
        AddHandler TextBox1.TextChanged, AddressOf ValidateInputs
        AddHandler TextBox2.TextChanged, AddressOf ValidateInputs
        'this is for button not to enable when textbox is empty
    End Sub
    Private Sub Timer1_Tick_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Label3.Text = (DateTime.Now.ToLongTimeString() + "," + DateTime.Now.ToLongDateString())
        'for timer
    End Sub

    Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
        ProgressBar1.Increment(30)
        If ProgressBar1.Value = ProgressBar1.Maximum Then
            Timer2.Stop()
            MessageBox.Show("Welcome to Billing System", "", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Menudata.Show()
            Me.Hide()
        End If
    End Sub
End Class

Your problem lies in the fact that you check the user name and password, starting the timer and progress bar if they are correct, and then continue on to increment ctr and crr anyway. If the user name and password is accepted you want to step out of that code and move onto the next thing.
The easiest solution is to wrap the two code blocks in another if statement. If user name /password OK then continue else ctr increments.

Your problem lies in the fact that you check the user name and password, starting the timer and progress bar if they are correct, and then continue on to increment ctr and crr anyway. If the user name and password is accepted you want to step out of that code and move onto the next thing.
The easiest solution is to wrap the two code blocks in another if statement. If user name /password OK then continue else ctr increments.

I couldn't agree more with hericles. Review your logic and problem solved.

Its a matter of arranging the code . You have everything needed for the solution.

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.