Results 1 to 10 of 10

Thread: [RESOLVED] read a comma delimited txt file into a table

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Oct 2006
    Location
    In the midst of corn, cotton, and beans
    Posts
    185

    Resolved [RESOLVED] read a comma delimited txt file into a table

    OK, I haven't worked with VB in about two years so I'm rusty and wasn't very good to begin with.

    I have a .txt file that is storing a sequential records and need those records read into a vb table (15 colums, 81 rows). Any suggestions on where to start.

  2. #2

  3. #3
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: read a comma delimited txt file into a table

    Something like this should do:
    VB Code:
    1. Private Sub Command1_Click()
    2.     Open FileName For Input As #1
    3.     Do While Not EOF(1)
    4.         Input #1, str1, str2, str3, str4, str5, str6, str7, str8, str9, str10, str11, str12, str13, str14, str15
    5.         '
    6.         'Add code here to fill these variables into your grid cells
    7.         '
    8.     Loop
    9.     Close #1
    10. End Sub
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  4. #4
    Frenzied Member
    Join Date
    Oct 2003
    Posts
    1,301

    Re: read a comma delimited txt file into a table

    This creates a 2D array of strings.
    You specify the file to load and what you want to use as a delimiter.
    VB Code:
    1. Function LoadCSV(Path As String, Optional Delim As String = ",") As String()
    2. Dim ipf As Long
    3. Dim strIn() As String
    4. Dim strTmp() As String
    5. Dim strOut() As String
    6. Dim row As Long
    7. Dim column As Long
    8.    
    9.     If Dir(Path) <> "" Then                'test if file exists
    10.        
    11.         ipf = FreeFile                     '
    12.         Open Path For Binary As #ipf       'open the file
    13.         ReDim strIn(0)                     '
    14.         strIn(0) = String(LOF(ipf), " ")   'create a buffer for the data
    15.         Get #ipf, , strIn(0)               'load the data into the buffer
    16.         strIn = Split(strIn(0), vbCrLf)    'split the data into individual lines
    17.         Close #ipf                         '
    18.          
    19.         ReDim strOut(UBound(strIn), 0)     '
    20.            
    21.         For row = 0 To UBound(strOut)      'loop through all lines
    22.            
    23.             strTmp = Split(strIn(row), Delim)                             'split the line into individual fields
    24.             strIn(row) = vbNullString                                     '
    25.             If UBound(strOut, 2) < UBound(strTmp) Then                    'if the current line has more fields than our array has columns
    26.                 ReDim Preserve strOut(UBound(strOut, 1), UBound(strTmp))  'redim our array with more columns
    27.             End If                                                        '
    28.            
    29.             For column = 0 To UBound(strTmp)                              'write all fields to the array
    30.                 strOut(row, column) = strTmp(column)                      '
    31.             Next column                                                   '
    32.            
    33.         Next row                            '
    34.        
    35.     End If
    36.  
    37.     LoadCSV = strOut   'return the array
    38.    
    39. End Function
    Last edited by jeroen79; Nov 1st, 2006 at 04:27 AM.

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Oct 2006
    Location
    In the midst of corn, cotton, and beans
    Posts
    185

    Re: read a comma delimited txt file into a table

    Jeroen
    The code worked lik a charm.
    Do you think you or anyone could add comments. As I don't follow exactly what's going on.

    My next step is to write from the table back to the csv.

  6. #6
    Addicted Member
    Join Date
    Oct 2006
    Posts
    172

    Re: read a comma delimited txt file into a table

    http://www.vbforums.com/showthread.php?t=433735 might also help with your reading of *.csv files. (just look towards the end)

  7. #7
    Frenzied Member
    Join Date
    Oct 2003
    Posts
    1,301

    Re: read a comma delimited txt file into a table

    Done.

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Oct 2006
    Location
    In the midst of corn, cotton, and beans
    Posts
    185

    Re: read a comma delimited txt file into a table

    OK, how would you write the array back to the .csv keeping the same format?

    stored in Array(x,x) as string

    Array(0,0) = 1
    Array(0,1) = 12
    Array(0,2) = 123
    Array(1,0) = a
    Array(1,1) = ab
    Array(1,3) = abc


    Output to .csv

    1,12,123
    a,ab,abc

  9. #9
    Frenzied Member
    Join Date
    Oct 2003
    Posts
    1,301

    Re: read a comma delimited txt file into a table


  10. #10
    Addicted Member
    Join Date
    Jan 2006
    Location
    Bangalore
    Posts
    172

    Re: read a comma delimited txt file into a table

    http://www.vbforums.com/showthread.p...75#post2679775
    Please have a look


    Quote Originally Posted by jeroen79
    This creates a 2D array of strings.
    You specify the file to load and what you want to use as a delimiter.
    VB Code:
    1. Function LoadCSV(Path As String, Optional Delim As String = ",") As String()
    2. Dim ipf As Long
    3. Dim strIn() As String
    4. Dim strTmp() As String
    5. Dim strOut() As String
    6. Dim row As Long
    7. Dim column As Long
    8.    
    9.     If Dir(Path) <> "" Then                'test if file exists
    10.        
    11.         ipf = FreeFile                     '
    12.         Open Path For Binary As #ipf       'open the file
    13.         ReDim strIn(0)                     '
    14.         strIn(0) = String(LOF(ipf), " ")   'create a buffer for the data
    15.         Get #ipf, , strIn(0)               'load the data into the buffer
    16.         strIn = Split(strIn(0), vbCrLf)    'split the data into individual lines
    17.         Close #ipf                         '
    18.          
    19.         ReDim strOut(UBound(strIn), 0)     '
    20.            
    21.         For row = 0 To UBound(strOut)      'loop through all lines
    22.            
    23.             strTmp = Split(strIn(row), Delim)                             'split the line into individual fields
    24.             strIn(row) = vbNullString                                     '
    25.             If UBound(strOut, 2) < UBound(strTmp) Then                    'if the current line has more fields than our array has columns
    26.                 ReDim Preserve strOut(UBound(strOut, 1), UBound(strTmp))  'redim our array with more columns
    27.             End If                                                        '
    28.            
    29.             For column = 0 To UBound(strTmp)                              'write all fields to the array
    30.                 strOut(row, column) = strTmp(column)                      '
    31.             Next column                                                   '
    32.            
    33.         Next row                            '
    34.        
    35.     End If
    36.  
    37.     LoadCSV = strOut   'return the array
    38.    
    39. End Function

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