Solved

Filter a textfile

Posted on 2004-04-07
3
300 Views
Last Modified: 2010-05-02
I'm rather new to vb6 and experience some problems on how to filter a textfile.
I have a text file wich I want to import in acces through vb.
This is not a problem, but before I want to import the text file I need to filter the text file.
What I want is to replace the fields containing a value of "120" with "E13" and replace the fields containing "sp001" with " " (nothing, just delete the value)
The textfile is comma seperated...

Thanks in advance

Best regards,
Boomer

0
Comment
Question by:boomer001
  • 2
3 Comments
 
LVL 14

Expert Comment

by:aelatik
ID: 10773913
Const SOURCE As String = "c:\file1.txt"
Const TARGET As String = "c:\file2.txt"

Open SOURCE For Input As #1
    Open TARGET For Output As #2
    While Not EOF(1)
        Line Input #1, MyLine
        Print #2, Replace(Replace(MyLine, "120", "E13"), "sp001", "")
    Wend
    Close #2
Close #1
0
 
LVL 11

Accepted Solution

by:
dbrckovi earned 50 total points
ID: 10773927
Try this:

---------------------------------------------------------------------------
Function FileFilter(ByVal FileName As String) As Boolean
    On Error Resume Next
    Dim FileLine() As String
   
    ReDim FileLine(0 To 0)
    Open FileName For Input As #1
        Do
            ReDim Preserve FileLine(0 To UBound(FileLine) + 1)
            Line Input #1, FileLine(UBound(FileLine))
        Loop Until EOF(1)
    Close #1
    'file is now saved in line array
   
    For X = 1 To UBound(FileLine)
        FileLine(X) = Replace(FileLine(X), "120", "E13")
        FileLine(X) = Replace(FileLine(X), "sp001", " ")
    Next X
   
    Open FileName For Output As #1
        For X = 1 To UBound(FileLine)
            Print #1, FileLine(X)
        Next X
    Close #1
   
    If Err = 0 Then         'return False if error occures
        FileFilter = True
    Else
        FileFilter = False
    End If

End Function
----------------------------------------------------------------------
To use you can type:  
     MsgBox  FileFilter ("e:\test\aa.txt")

If it was fully succesfull, it will return True, otherwise it will return False
0
 
LVL 11

Expert Comment

by:dbrckovi
ID: 10778548
Thanks!
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

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

914 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

21 Experts available now in Live!

Get 1:1 Help Now