Solved

Filter a textfile

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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Need VBS version of Excel date conversion 4 71
VBA/SQL - Connect to SQL server and pull data 4 109
Using "ScreenUpdating" 6 64
Add a task in Outlook from access 11 39
There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
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…

772 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