Solved

Filter a textfile

Posted on 2004-04-07
3
299 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
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…

747 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

13 Experts available now in Live!

Get 1:1 Help Now