Solved

Changing data in a Tab Delimited text files

Posted on 2004-08-01
2
294 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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

707 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

18 Experts available now in Live!

Get 1:1 Help Now