Results 1 to 10 of 10

Thread: Loading from a flat (txt) file to Listbox

  1. #1

    Thread Starter
    Member
    Join Date
    Sep 2005
    Posts
    54

    Loading from a flat (txt) file to Listbox

    I have an input file similar to the one below:-

    - The first 6 characters (1,6) for all rows would be the same . Following this
    - in the first row (7,1 st position) there will be some 1 digit
    - in the second row there will be EOL(7,3 rd position), followed by 9 Characters (H6E016094)(10,9 th position)
    - in the third row there will be 95 followed by 7 digits (4125159) (9,7 th position)

    2188571
    218857EOLH6E016094
    218857954125159

    How do I strip these fields from file and diplay that in a form inside a List Box. Below is how I expect to see the value from the file to be displayed.

    Column1 Column2 Column3 Column4
    1 EOL H6E016094 954125159
    Is using Listbox is the correct approach or how do I go about?
    Attached Files Attached Files
    Last edited by sakreg1; Nov 15th, 2005 at 12:06 PM.

  2. #2

  3. #3

    Thread Starter
    Member
    Join Date
    Sep 2005
    Posts
    54

    Re: Loading from a flat (txt) file to Listbox

    I am not familar with neither ListBox nor Grid. I leave the choice to you guys for which one would be better and easy to understand

  4. #4

  5. #5

    Thread Starter
    Member
    Join Date
    Sep 2005
    Posts
    54

    Re: Loading from a flat (txt) file to Listbox

    First post edited with the attachment

  6. #6
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: Loading from a flat (txt) file to Listbox

    I see your sample but that's not what I meant. I'm looking for an actual data file.

    In your explanation you seem to want to put data from the first 3 lines in the file on the same line in the listbox/listview. Is that correct? If so is there more data and is all the data in sets of 3 rows?

  7. #7
    Frenzied Member trisuglow's Avatar
    Join Date
    Jan 2002
    Location
    Horsham, Sussex, UK
    Posts
    1,536

    Re: Loading from a flat (txt) file to Listbox

    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Command1_Click()
    4.   Dim f As Integer
    5.   Dim i As Integer
    6.   Dim s As String
    7.   f = FreeFile
    8.   Open "C:\sample.txt" For Input As f
    9.   For i = 1 To 3
    10.     Line Input #f, s
    11.     Select Case i
    12.     Case 1
    13.       Debug.Print Mid$(s, 7, 1)
    14.     Case 2
    15.       Debug.Print Mid$(s, 7, 3)
    16.       Debug.Print Mid$(s, 10, 9)
    17.     Case 3
    18.       Debug.Print Mid$(s, 7, 9)
    19.     End Select
    20.   Next
    21.   Close f
    22.  
    23. End Sub
    This world is not my home. I'm just passing through.

  8. #8

    Thread Starter
    Member
    Join Date
    Sep 2005
    Posts
    54

    Re: Loading from a flat (txt) file to Listbox

    All the data to be populated for one row is there in the file. Always data from set of 3 rows in line will form a one row in the listbox. If I go with more data in the sample file, that won't help much I think Martin.

  9. #9
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: Loading from a flat (txt) file to Listbox

    Well it would help to test it better, but try this.

    VB Code:
    1. Dim ff As Integer
    2.     Dim strLine As String
    3.     Dim clmX As ColumnHeader
    4.     Dim itmX As ListItem
    5.     Dim intPart As Integer
    6.  
    7.     ' Add ColumnHeaders.
    8.     Set clmX = ListView1.ColumnHeaders.Add(, , "Col 1", 900)
    9.     Set clmX = ListView1.ColumnHeaders.Add(, , "Col 2", 900)
    10.     Set clmX = ListView1.ColumnHeaders.Add(, , "Col 3", 1200)
    11.     Set clmX = ListView1.ColumnHeaders.Add(, , "Col 4", 1200)
    12.    
    13.     ListView1.BorderStyle = ccFixedSingle ' Set BorderStyle property.
    14.     ListView1.View = lvwReport ' Set View property to Report.
    15.  
    16.     ff = FreeFile
    17.    
    18.     Open "C:\temp\test.txt" For Input As #ff
    19.    
    20.     Do Until EOF(ff)
    21.         For intPart = 1 To 3
    22.             Select Case intPart
    23.                 Case 1
    24.                     Line Input #ff, strLine
    25.                     Set itmX = ListView1.ListItems.Add(, , Mid$(strLine, 7, 1))
    26.                     Line Input #ff, strLine
    27.                     itmX.SubItems(1) = Mid$(strLine, 7, 3)
    28.                 Case 2
    29.                     itmX.SubItems(2) = Mid$(strLine, 10)
    30.                 Case 3
    31.                     Line Input #ff, strLine
    32.                     itmX.SubItems(3) = Mid$(strLine, 7)
    33.             End Select
    34.         Next
    35.     Loop
    36.    
    37.     Close ff

  10. #10

    Thread Starter
    Member
    Join Date
    Sep 2005
    Posts
    54

    Re: Loading from a flat (txt) file to Listbox

    Will try out that. ThankYou for all the responses.

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