?
Solved

Reading Data From Sequential File

Posted on 2003-03-24
9
Medium Priority
?
276 Views
Last Modified: 2010-04-07
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
0
Comment
Question by:Attapol
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +2
9 Comments
 

Expert Comment

by:scouto
ID: 8195606
Send me the line code were you use then function "Additem"
0
 
LVL 2

Expert Comment

by:Jacamar
ID: 8195738
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.
0
 

Expert Comment

by:scouto
ID: 8195835
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.



0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 2

Expert Comment

by:Jacamar
ID: 8195962
whoops....typo.....thanks scouto
0
 

Author Comment

by:Attapol
ID: 8200934
Thanks all,

I got stuck with Do until EOF()

It shows the error message.

Attapl
0
 

Author Comment

by:Attapol
ID: 8201409
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
0
 
LVL 2

Accepted Solution

by:
Jacamar earned 200 total points
ID: 8203039
Ok, because you are inputting 4 variables at a time, you need to make sure that the file is complete.  Also, that there are commas between all values, except not at the end of each line.

Again, you should change the Write #1 statement to Print #1.
0
 

Expert Comment

by:CleanupPing
ID: 8531733
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.
0
 
LVL 6

Expert Comment

by:GPrentice00
ID: 9441175
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
0

Featured Post

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question