[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

Reading Data From Sequential File

Posted on 2003-03-24
9
Medium Priority
?
284 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
  • 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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
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

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

Question has a verified solution.

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

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

612 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