Do while loop

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

    Do while loop

    I have a do while loop thats reading from a file. I have two forms in my program and the file is reading four lines the acct number, which is entered in the prior form as a login. The lines in the file each have a different account number above them. I want to read only the files below the certain account number.

    file setup(lines in the txt file)
    987654
    D
    60
    11/22/2007
    987654
    D
    60
    11/23/2007
    987654
    D
    45
    11/24/2007
    987654
    W
    79
    11/26/2007

    etc...

    I am reading the file to get a sum for that account number the D's are deposits and W's are withdrawals.
    Code:
     Dim sr As IO.StreamReader
        Dim Acctname, Acctaddress, Acctnumber, sum As String
    
    
        Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Me.Text = "RBD Bank"
            OpenFile()
            Do While (Acctnumber <> frmLogin.txtaccount.Text) And (sr.Peek <> -1)
                Acctnumber = sr.ReadLine
                Acctname = sr.ReadLine
                Acctaddress = sr.ReadLine
            Loop
            
            Dim fmtstr As String = "{0,-20}"
            lstaddress.Text = lstaddress.Items.Add(String.Format(fmtstr, Acctnumber))
            lstaddress.Text = lstaddress.Items.Add(String.Format(fmtstr, Acctname))
            lstaddress.Text = lstaddress.Items.Add(String.Format(fmtstr, Acctaddress))
            lstaddress.Show()
            CloseFile()
        End Sub
       
        Sub OpenFile() 'Opens the File, make sure it is in the Bin folder
            sr = IO.File.OpenText("accounts.txt")
        End Sub
        Sub OpenFile2() 'Opens the File, make sure it is in the Bin folder
            sr = IO.File.OpenText("transactions.txt")
        End Sub
        Sub CloseFile() 'Closes the file
            sr.Close()
        End Sub
    
    
    
        Private Sub btnAcctBalance_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAcctBalance.Click
            Dim Transtype, amount, transdate, acctnum As String
            OpenFile2()
            Do Until acctnum <> Acctnumber    'I think the problem is right here but dunno how to tell it to read only the account number I entered
                acctnum = sr.ReadLine()
                Transtype = sr.ReadLine()
                amount = sr.ReadLine()
                transdate = sr.ReadLine()
                If Transtype = "W" Then
                    sum -= CDbl(amount)
                ElseIf Transtype = "D" Then
                    sum += CDbl(amount)
                End If
            Loop
            CloseFile()
            MsgBox(sum, MsgBoxStyle.Information, "Current Account Balance")
        End Sub
    
        Private Sub btnexit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnexit.Click
            Me.Close()
            frmLogin.Close()
        End Sub
    End Class
    I think i am needing something different in line 37
  • nabman
    New Member
    • Nov 2007
    • 7

    #2
    I figured it out, needed a nested if statement to verify the correct account number.

    Comment

    Working...