Validation, Exit Loop

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nabman
    New Member
    • Nov 2007
    • 7

    Validation, Exit Loop

    I am reading a txt file, I have a txt file(customers. txt) that is read, with account numbers and passwords. Once the user enters a account number and password it should read the file and if there in there it will show the second form and end the loop. The probelm I am having is when I enter a acct # and password that is maybe the third entry in the file it is hitting the "else" statment and displaying my msgbox. What is the code I can put in front of the if statement to tell it to keep reading until the end of the file then display the error message? Below is what I have got so far, Thanks for you help.

    Code:
    Dim sr As IO.StreamReader
        Dim Acctnumber, password As String
        Private Sub frmBankATM_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Me.Text = "RBD Bank Account Login"
        End Sub
    
        Sub OpenFile()
            sr = IO.File.OpenText("customers.txt")
        End Sub
        Sub CloseFile()
            sr.Close()
        End Sub
    
        Private Sub btnSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
            If txtaccount.Text = "" Then
                MsgBox("Please Enter a Account Number.", MsgBoxStyle.Information, "Account a password")
                txtaccount.Focus()
                Exit Sub
            End If
            If txtpassword.Text = "" Then
                MsgBox("Please Enter a Password.", MsgBoxStyle.Information, "Password")
                txtpassword.Focus()
                Exit Sub
            End If
    
            OpenFile()
            Do While sr.Peek <> -1
                Acctnumber = sr.ReadLine
                password = sr.ReadLine
                If txtaccount.Text = Acctnumber And txtpassword.Text = Reverse(password) Then
                    frmMainboard.Show()
                    Exit Sub
                ElseIf sr.Peek <> -1 Then
                    'ElseIf txtaccount.Text <> Acctnumber And txtpassword.Text <> password Then
                    MsgBox("Invalid Account Number and/or password combination", MsgBoxStyle.Critical, "Invalid Account number or Password")
                    txtpassword.Focus()
                End If
            Loop
    
            CloseFile()
        End Sub
    
        Function Reverse(ByVal info As String) As String
            Dim a, b As Integer
            Dim temp As Integer
            Dim password As String = ""
            a = info.Length
            For b = a - 1 To 0 Step -1
                temp &= info.Substring(b, 1)
            Next
            Return temp
        End Function
    End Class
  • kadghar
    Recognized Expert Top Contributor
    • Apr 2007
    • 1302

    #2
    use a boolean, check all the names, if you find the name and psswrd then the boolean will be true, else it will keep false
    after you have checked all names, you can give acces if the boolean is true, or deni it if its false

    this is the idea
    [CODE=vb]
    dim boo1 as boolean
    do
    if name(i) = enteredName and pass(i) = enteredPasswd then
    boo1= true
    exit do
    end if
    i=i+1
    loop until i > ubound(name)

    if boo1=true then
    'Whatever it does when password is fine
    else
    msgbox "Name and Password are incorrect"
    end if[/CODE]

    well HTH

    Comment

    • nabman
      New Member
      • Nov 2007
      • 7

      #3
      Thanks for your help, I got it to work.

      Comment

      Working...