Link to home
Start Free TrialLog in
Avatar of BrendonG
BrendonG

asked on

Changing data in a Tab Delimited text files

I have a tab delimited text file that contains 26 fields and anywhere from 1 upwards rows.

The rows can contain information in all 26 fields or only the first field.

The sixth field is currently empty (Class).   If the first column contains TRNS or SPL I need to put information into the sixth field and copy this to a new tab delimited text file (or update the original).

I have been able to read the test file into a recordset, however this recordset will not allow me to update field (5).   I have also writen script to write from the first recordset toa new tab delimited file.   Can some one tell me how when writing to the new file, I will be able to add information to field 5 of the recordset if the above requirements are met.  

Regards

brendon Gardiner  
Avatar of aelatik
aelatik
Flag of Netherlands image

Here you go,

Private Sub Form_Load()

    Const FILENAME As String = "c:\temp.csv" ' the file to read
    Const NEWFILENAME As String = "c:\temp1.csv" ' the newfile to create
   
    Dim FNUM As Integer
    Dim FNUM1 As Integer
    Dim TEMP As String
    Dim FIELDS() As String
    Dim I As Long
    Dim NEWLINE As String
   
        FNUM = FreeFile
        FNUM1 = FreeFile
        Open FILENAME For Input As #1
        Open NEWFILENAME For Output As #2
       
            While Not EOF(1)
                Line Input #1, TEMP
                FIELDS = Split(TEMP, vbTab)
                If FIELDS(0) = "TRNS" Or FIELDS(0) = "SPL" Then ' check for criteria
                    FIELDS(5) = "TheNewValue" ' set the new value
                End If
                NEWLINE = ""
               ' construct the new line
                For I = 0 To UBound(FIELDS)
                    NEWLINE = NEWLINE & FIELDS(I)
                    If UBound(FIELDS) <> I Then NEWLINE = NEWLINE & vbTab
                Next
                Print #2, NEWLINE
            Wend
        Close #1
        Close #2
End Sub
ASKER CERTIFIED SOLUTION
Avatar of aelatik
aelatik
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial