Results 1 to 25 of 25

Thread: How do I put a txt document in a listbox?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Sep 2002
    Posts
    65

    How do I put a txt document in a listbox?

    Does anyone know how I am supposed to put a txt document called payroll.txt in a listbox called lstemployees. Once I have the info put in a i have to get certain parts of it and have them put in seperate text boxes. Here is an example of a couple of lines of info to be put in the listbox:

    Klinton Carpenter 18.00
    *Cynthia Crenshaw 18.00
    Richard Crouse 19.00

    After that info is in the listbox. I need to put the FIRST NAME, LAST NAME, and NUMBER and INITIALS in seperate textboxes.
    Can anyone help me with this? I've tried everything.

  2. #2
    PowerPoster
    Join Date
    Aug 2002
    Location
    NY, NY
    Posts
    2,139
    [Highlight=VB]
    Dim strLine As String

    Open "myfile_path" for Output As #1
    Do
    Line Input #1, strLine
    List1.AddItem strLine
    Loop Until EOF(1)
    Close #1
    Roy

  3. #3
    PowerPoster
    Join Date
    Aug 2002
    Location
    NY, NY
    Posts
    2,139
    Sorry, I hit Send too early, but anyway, to simplify processing your file layout must be consistent or you'll have a major headach otherwise.
    Roy

  4. #4
    PowerPoster
    Join Date
    Aug 2002
    Location
    NY, NY
    Posts
    2,139
    It may look something like the following:
    VB Code:
    1. Dim strLine As String
    2. Dim Pos%
    3.  
    4.     Open "myfile_path" For Output As #1
    5.         Do
    6.             Line Input #1, strLine
    7.             'if layout is always as follows: "FirstName LastName 18.00" then
    8.             Pos = InStrRev(strLine, Space(1))
    9.             List1.AddItem Left(strLine, Pos - 1)
    10.             Pos = InStr(1, Left(strLine, Pos - 1), Space(1))
    11.             Text1.Text = UCase(Left(strLine, 1) & Mid(strLine, Pos + 1, 1))
    12.             Text2.Text = Mid(strLine, Pos + 1)
    13.         Loop Until EOF(1)
    14.     Close #1
    Roy

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Sep 2002
    Posts
    65
    hi, what is the "pos%"?

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Sep 2002
    Posts
    65
    Can anyone help me out with this? I still can't get this working...

  7. #7
    PowerPoster
    Join Date
    Aug 2000
    Location
    IN SILENCE
    Posts
    6,441

    Well

    VB Code:
    1. Open SFILE For Input As #1
    2.                    
    3.     Dim STRING1 As String, STRING2 As String
    4.                                
    5.     Do Until EOF(1) = True
    6.  
    7.         Input #1, STRING1, STRING2
    8.                    
    9.         Msgbox String1 & " - " & String2
    10.                    
    11.     Loop
    12.                                
    13.     Close #1

    Is this what you are after?
    Remaining quiet down here !!!

    BRAD HAS GIVEN ME THE ULTIMATIVE. I have chosen to stay....

  8. #8
    PowerPoster
    Join Date
    Aug 2002
    Location
    NY, NY
    Posts
    2,139
    Dim pos% is the same as
    Dim pos As Integer

    What isn't working for you? Whatever you've done - post it, so we have a chance to see what's wrong, otherwise is like pointing to the sky and tring to find your star.
    Roy

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Sep 2002
    Posts
    65
    Dim strLine As String
    Dim intPos As Integer

    Open "c:/student docs/payroll.txt" For Output As #1
    Do
    Line Input #1, strLine ****
    intPos = InStrRev(strLine, Space(1))
    lstEmployees.AddItem Left(strLine, intPos - 1)
    intPos = InStr(1, Left(strLine, intPos - 1), Space(1))
    txtFirstName.Text = UCase(Left(strLine, 1) & Mid(strLine, intPos + 1, 1))
    txtLastName.Text = Mid(strLine, Pos + 1)
    Loop Until EOF(1)
    Close #1


    ****I get an error here "bad file mode"

  10. #10
    PowerPoster
    Join Date
    Aug 2002
    Location
    NY, NY
    Posts
    2,139
    For Output As #1

    Change it to For Input
    Roy

  11. #11
    PowerPoster
    Join Date
    Aug 2002
    Location
    NY, NY
    Posts
    2,139
    When you open file For Output - you are actually outputting from VB into a file
    When file is open For Input - data will be Inputed from the file into a variable
    Roy

  12. #12

    Thread Starter
    Lively Member
    Join Date
    Sep 2002
    Posts
    65
    Ok, I ran it after I changed it and it said... "input past end of file".... AHHHHH!

  13. #13
    PowerPoster
    Join Date
    Aug 2002
    Location
    NY, NY
    Posts
    2,139
    Of course - your previous attempt erased it (For Output clears file almost immediately). Restore file and try again.
    Roy

  14. #14

    Thread Starter
    Lively Member
    Join Date
    Sep 2002
    Posts
    65
    How do I restore file? I'm so sorry for all the questions.

  15. #15

    Thread Starter
    Lively Member
    Join Date
    Sep 2002
    Posts
    65
    Hi, I got the names to get in the lstbox...but not the numbers by their names. This is what I have. Does anyone have any ideas as to why?

    Open "c:/student docs/payroll.txt" For Input As #1
    Do
    Line Input #1, strLine
    intPos = InStrRev(strLine, Space(1))
    lstEmployees.AddItem Left(strLine, intPos - 1)
    Loop Until EOF(1)
    Close #1


    This is what is supposed to be in the listbox

    Klinton Carpenter 18.00
    *Cynthia Crenshaw 18.00
    Richard Crouse 19.00

  16. #16
    PowerPoster jdc2000's Avatar
    Join Date
    Oct 2001
    Location
    Idaho Falls, Idaho USA
    Posts
    2,526
    VB Code:
    1. ' The following line opens the text file for input into your VB program
    2. Open "c:/student docs/payroll.txt" For Input As #1
    3. ' Start a loop
    4. Do
    5.     ' Get a line of text file the file
    6.     Line Input #1, strLine
    7.     ' Find the last space in the line of text
    8.     intPos = InStrRev(strLine, Space(1))
    9.     ' Add the name (without the amount) to the list box
    10.     lstEmployees.AddItem Left(strLine, intPos - 1)
    11.     ' Add the name AND amount to the list box
    12.     ' (un-comment this line and comment the previous AddItem line to see the difference.)
    13.     ' lstEmployees.AddItem strLine
    14. ' Continue the loop until the end of file is reached
    15. Loop Until EOF(1)
    16. ' Close the file
    17. Close #1

  17. #17

    Thread Starter
    Lively Member
    Join Date
    Sep 2002
    Posts
    65
    Thanks!! It worked!! Now can you help me put the first name, last name, initials and amount in seperate textboxes. I got them all done except the amount. For some reason it comes out in the Last name text box. I also have to make all this info change when the user highlights a different name. Can you help me with this? This is what I have so far:

    Open "c:/student docs/payroll.txt" For Input As #1
    Do
    Line Input #1, strLine
    intPos = InStrRev(strLine, Space(3))
    lstEmployees.AddItem strLine
    intPos = InStr(1, Left(strLine, intPos - 1), Space(1))
    txtFirstName.Text = Left(strLine, intPos + 0)
    txtLastName.Text = Mid(strLine, intPos + 1)
    txtInitials.Text = UCase(Left(strLine, 1) & Mid(strLine, intPos + 1, 1))
    Loop Until EOF(1)
    Close #1

  18. #18
    PowerPoster jdc2000's Avatar
    Join Date
    Oct 2001
    Location
    Idaho Falls, Idaho USA
    Posts
    2,526
    The following should get the correct data into your text boxes:

    VB Code:
    1. ' The following line opens the text file for input into your VB program
    2. Open "c:/student docs/payroll.txt" For Input As #1
    3. ' Start a loop
    4. Do
    5.     ' Get a line of text file the file
    6.     Line Input #1, strLine
    7.     ' Find the last space in the line of text
    8.     intPos = InStrRev(strLine, Space(1))
    9.     ' Add the name (without the amount) to the list box
    10.     ' lstEmployees.AddItem Left(strLine, intPos - 1)
    11.     ' Add the name AND amount to the list box
    12.     ' (un-comment this line and comment the previous AddItem line to see the difference.)
    13.     ' lstEmployees.AddItem strLine
    14.     intPos = InStrRev(strLine, Space(3))
    15.     lstEmployees.AddItem strLine
    16.     intPos2 = InStr(1, Left(strLine, intPos - 1), Space(1))
    17.     txtFirstName.Text = Left(strLine, intPos - 1)
    18.     intPos3 = InStr(intPos2, Left(strLine, intPos - 1), Space(1))
    19.     txtLastName.Text = Mid(strLine, intPos2 + 1, (intPos3 - 1) - intPos2)
    20.     ' txtInitials.Text = UCase(Left(strLine, 1) & Mid(strLine, intPos + 1, 1))
    21.     txtInitials.Text = UCase(Left(txtFirstName.Text, 1) & UCase(Left(txtLastName.Text, 1))
    22.     txtAmount.Text = Trim(Right(strLine, Len(strLine) - intPos3))
    23. ' Continue the loop until the end of file is reached
    24. Loop Until EOF(1)
    25. ' Close the file
    26. Close #1

  19. #19
    PowerPoster jdc2000's Avatar
    Join Date
    Oct 2001
    Location
    Idaho Falls, Idaho USA
    Posts
    2,526
    To get the text box contents to change when a list box item is selected, you will need to move the code to put the data into them into the list box's 'click' event sub:

    VB Code:
    1. Private Sub lstEmployees_Click()
    2.  
    3.     Dim strLineSelected As String
    4.    
    5.     If lstEmployees.ListIndex < 0 Then Exit Sub
    6.     strLineSelected = lstEmployees.List(lstEmployees.ListIndex)
    7.     intPos = InStrRev(strLineSelected, Space(3))
    8.     intPos2 = InStr(1, Left(strLineSelected, intPos - 1), Space(1))
    9.     txtFirstName.Text = Left(strLineSelected, intPos - 1)
    10.     intPos3 = InStr(intPos2, Left(strLineSelected, intPos - 1), Space(1))
    11.     txtLastName.Text = Mid(strLineSelected, intPos2 + 1, (intPos3 - 1) - intPos2)
    12.     ' txtInitials.Text = UCase(Left(strLineSelected, 1) & Mid(strLineSelected, intPos + 1, 1))
    13.     txtInitials.Text = UCase(Left(txtFirstName.Text, 1) & UCase(Left(txtLastName.Text, 1))
    14.     txtAmount.Text = Trim(Right(strLineSelected, Len(strLineSelected) - intPos3))
    15.  
    16. End Sub

  20. #20

    Thread Starter
    Lively Member
    Join Date
    Sep 2002
    Posts
    65
    Thank you so much for your help. It is very appreciated. I tried that. The hourly rate shows the Last Name and the hourly rate. The First name shows the First and Last name. The last name shows nothing. And the initials shows the first initial only.

  21. #21
    Fanatic Member
    Join Date
    Feb 2002
    Location
    SE England
    Posts
    732
    Try this:


    Code:
    Private Sub lstEmployees_Click()
    
        Dim sinfo() As String
        
        If lstEmployees.ListIndex < 0 Then Exit Sub
        
        sInfo = Split(lstEmployees.List(lstEmployees.ListIndex)," ")
       
        txtFirstName.Text = sInfo(0)
        txtLastName.Text = sInfo(1)
    
        txtInitials.Text = Left(sInfo(0),1) & " " & Left(sInfo(1),1) 
    
    
        txtAmount.Text = sinfo(2)
    
    End Sub
    Leather Face is comin...


    MCSD

  22. #22

    Thread Starter
    Lively Member
    Join Date
    Sep 2002
    Posts
    65
    wow! Thanks!! that worked. except for the hourly rate. It doesnt show up at all.

  23. #23
    Fanatic Member
    Join Date
    Feb 2002
    Location
    SE England
    Posts
    732
    Hmm...

    Try putting a break point on this line and however the mouse over sInfo(2).. What is in it?


    txtAmount.Text = sinfo(2)
    Leather Face is comin...


    MCSD

  24. #24

    Thread Starter
    Lively Member
    Join Date
    Sep 2002
    Posts
    65
    whats a break point? sorry i'm a newbie

  25. #25
    Fanatic Member
    Join Date
    Feb 2002
    Location
    SE England
    Posts
    732
    Highlight the line in the code editor and press F9.

    This means that the program will stop executing at this line...

    Then run your program, click on the list box and it will stop... and you can find out what is actually going on.
    Leather Face is comin...


    MCSD

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width