Link to home
Start Free TrialLog in
Avatar of Attapol
Attapol

asked on

Reading Data From Sequential File

Hi all,

I'm writting Expense Tracking Program by VB 6.

When I add new record to sequential file by putting the same name.

When I open .txt file it looks like this,

"January", "John","Room",24.0
"January", "John","Food", 15.0
"February","Steve","Food", 20.0
"March","Jerry","Travel", 30.0

I use "AddItem" to put data to the combo box but when I click drop down list it shows two Johns.

When I choose each John, it shows the same output which is

January Room 24.0

Can anybody give me some advice or source code?

Thank you very much. ....Attapol
Avatar of scouto
scouto

Send me the line code were you use then function "Additem"
First of all....are you creating this sequential file?  Are the quotations necessary?  It looks like you are using the Write # function.  Use the Print # and there will be no quotations around the names.  That is your first step.  

Next you need to read in the data.

dim stString() as string

Private sub Command1_Click()

dim Filename as string
Filename = app.path & "\Text.txt"
Open Filename for Input as #2

Do until EOF()
inI = inI + 1
Redim stString (1 to 4, 1 to inI)
Input #2, stString(1,inI)
Input #2, stString(2,inI)
Input #2, stString(3,inI)
Input #2, stString(4,inI)
Loop

ok, now all of your data is in your array.

Now you want to load the names in to the Combo Box

For inJ = 1 to inI
  Call Combo1.AddItem  (stString(2,inI))
Next inJ

now, you want to have a combo 1 Change event

Private sub Combo1_Change()
dim stTemp as string
dim inCombo as integer
inCombo = Combo1.ListIndex + 1

stTemp = stString(2,inCombo)
Picture1.cls
For inJ = 1 to 30
  if stString(2,inJ) = stTemp then
   Picture1.Print stString(1,inJ), stString(3,inJ), stString(4,inJ)
  end if
Next inJ

End sub

This will find out which name is selected, and display the info regarding anyone with that name in a picture box.
tray this:

For inJ = 1 to inI
 Call Combo1.AddItem  (stString(2,inJ))
Next inJ

you are using inI but the currect is inJ because inI is a fixed value.



whoops....typo.....thanks scouto
Avatar of Attapol

ASKER

Thanks all,

I got stuck with Do until EOF()

It shows the error message.

Attapl
Avatar of Attapol

ASKER

Hi it's me again,
here is my source code...

For Adding data

Private Sub cmdSave_Click()
    Dim message As String
   'Add data to expenses2.txt
    If (cboExpMonth.Text <> "") And (txtName.Text <> "")And (cboExpType.Text <> "") And (txtAmt.Text <> "") Then
        Open "d:\my documents\my paper\expense2.txt" For Append As #1
        Write #1, cboExpMonth.Text, txtName.Text, cboExpType.Text, Val(txtAmt.Text)
        Close #1
        cboExpMonth.Text = ""
        txtName.Text = ""
        cboExpType.Text = ""
        txtAmt.Text = ""
        cboExpMonth.SetFocus
      Else
        message = "You must enter data."
        MsgBox message, , "Information incomplete"
    End If
End Sub

To load the names in to the combo box & display data


Private Sub cboExpMonth_Click()
 Dim ExpMonth As String, Name As String, ExpType As String, Amt As Single
    Open "d:\my documents\my paper\expense2.txt" For Input As #1
    Do While (ExpMonth <> cboExpMonth.Text) And (Not EOF(1))
        Input #1, ExpMonth, Name, ExpType, Amt
        picOutput.Cls
        picOutput.Print ExpMonth, Name, ExpType, Amt
    Loop
    Close #1
    If ExpMonth = cboExpMonth.Text Then
    picOutput.Print ExpMonth, Name, ExpType, Amt
    Else
    picOutput.Print "Not found"
    End If
      End Sub

Private Sub cboSelect_click()
    Dim ExpMonth As String, Name As String, ExpType As String, Amt As Single
    Open "d:\my documents\my paper\expense2.txt" For Input As #1
    Do While (Name <> cboSelect.Text) And (Not EOF(1))
        Input #1, ExpMonth, Name, ExpType, Amt
    Loop
    Close #1
    picOutput.Cls
    If Name = cboSelect.Text Then
        picOutput.Print ExpMonth, ExpType, Amt
        Else
        picOutput.Print "Not found"
    End If
End Sub

Private Sub cmdClose_Click()
    Close #1
    frmSearchExp.Hide
End Sub



Private Sub cmdExpType_Click()
 'Add Expense type to cboSelect
 Dim ExpMonth As String, Name As String, ExpType As String, Amt As Single
Open "d:\my documents\my paper\expense2.txt" For Input As #1
  lblSelect = "Expense Type:"
  cboSelect.Clear
  cboSelect.AddItem "Food"
  cboSelect.AddItem "Room"
  cboSelect.AddItem "Travel"
End Sub

Private Sub cmdPerson_Click()
    'Add Name to cboSelect
    Dim ExpMonth As String, Name As String, ExpType As String, Amt As Single
     Open "d:\my documents\my paper\expense2.txt" For Input As #1
     lblSelect = "Expense Person:"
     cboSelect.Clear
     Do While Not EOF(1)
      Input #1, ExpMonth, Name, ExpType, Amt
    cboSelect.AddItem Name
    Loop
    Close #1
End Sub

Please comment... Thanks a lot....Attapol
ASKER CERTIFIED SOLUTION
Avatar of Jacamar
Jacamar

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi Attapol,
This old question (QID 20560974) needs to be finalized -- accept an answer, split points, or get a refund.  Please see http://www.cityofangels.com/Experts/Closing.htm for information and options.
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

 -->Accept Jacamar's comment as Answer

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER

GPrentice00
Cleanup Volunteer