Solved

Changing data in a Tab Delimited text files

Posted on 2004-08-01
2
308 Views
Last Modified: 2010-05-02
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  
0
Comment
Question by:BrendonG
  • 2
2 Comments
 
LVL 14

Expert Comment

by:aelatik
ID: 11687493
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
0
 
LVL 14

Accepted Solution

by:
aelatik earned 500 total points
ID: 11687496
Clear the unneccesary declarations on this one :

Private Sub Form_Load()

    Const FILENAME As String = "c:\temp.csv"
    Const NEWFILENAME As String = "c:\temp1.csv"
   
    Dim TEMP As String
    Dim FIELDS() As String
    Dim I As Long
    Dim NEWLINE As String
   
        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 FIELDS(5) = "TheNewValue"
                NEWLINE = ""
                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
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB 6.0 printer how to align 6 58
Validating VB6 Function 19 56
Microsoft Access combo box help 2 38
fso.FolderExists("\\server\HiddenFolder$") 4 52
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…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

867 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now