By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,986 Members | 1,522 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,986 IT Pros & Developers. It's quick & easy.

Validation, Exit Loop

P: 7
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.

Expand|Select|Wrap|Line Numbers
  1. Dim sr As IO.StreamReader
  2.     Dim Acctnumber, password As String
  3.     Private Sub frmBankATM_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  4.         Me.Text = "RBD Bank Account Login"
  5.     End Sub
  6.  
  7.     Sub OpenFile()
  8.         sr = IO.File.OpenText("customers.txt")
  9.     End Sub
  10.     Sub CloseFile()
  11.         sr.Close()
  12.     End Sub
  13.  
  14.     Private Sub btnSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
  15.         If txtaccount.Text = "" Then
  16.             MsgBox("Please Enter a Account Number.", MsgBoxStyle.Information, "Account a password")
  17.             txtaccount.Focus()
  18.             Exit Sub
  19.         End If
  20.         If txtpassword.Text = "" Then
  21.             MsgBox("Please Enter a Password.", MsgBoxStyle.Information, "Password")
  22.             txtpassword.Focus()
  23.             Exit Sub
  24.         End If
  25.  
  26.         OpenFile()
  27.         Do While sr.Peek <> -1
  28.             Acctnumber = sr.ReadLine
  29.             password = sr.ReadLine
  30.             If txtaccount.Text = Acctnumber And txtpassword.Text = Reverse(password) Then
  31.                 frmMainboard.Show()
  32.                 Exit Sub
  33.             ElseIf sr.Peek <> -1 Then
  34.                 'ElseIf txtaccount.Text <> Acctnumber And txtpassword.Text <> password Then
  35.                 MsgBox("Invalid Account Number and/or password combination", MsgBoxStyle.Critical, "Invalid Account number or Password")
  36.                 txtpassword.Focus()
  37.             End If
  38.         Loop
  39.  
  40.         CloseFile()
  41.     End Sub
  42.  
  43.     Function Reverse(ByVal info As String) As String
  44.         Dim a, b As Integer
  45.         Dim temp As Integer
  46.         Dim password As String = ""
  47.         a = info.Length
  48.         For b = a - 1 To 0 Step -1
  49.             temp &= info.Substring(b, 1)
  50.         Next
  51.         Return temp
  52.     End Function
  53. End Class
  54.  
Nov 30 '07 #1
Share this Question
Share on Google+
2 Replies


kadghar
Expert 100+
P: 1,295
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
Expand|Select|Wrap|Line Numbers
  1. dim boo1 as boolean
  2. do
  3.     if name(i) = enteredName and pass(i) = enteredPasswd then
  4.        boo1= true
  5.        exit do
  6.     end if
  7.     i=i+1
  8. loop until i > ubound(name)
  9.  
  10. if boo1=true then 
  11.     'Whatever it does when password is fine
  12. else
  13.     msgbox "Name and Password are incorrect"
  14. end if
well HTH
Nov 30 '07 #2

P: 7
Thanks for your help, I got it to work.
Dec 1 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.