Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

READ TEXT FILE AND RETURN A RECORD VB

Posted on 2003-12-10
5
Medium Priority
?
25,197 Views
Last Modified: 2012-05-04
VB - i wanna search some specific text in a text file and return the record contening the text,  exemple :

i wanna search for this string : ''Milford''

i my result should be : ''4834544834569054713386    9054713386TANI08810039Markham   ON   2038784667     04550839Milford   CTA00010370000056000000000000000000T070219L  000240120031205104238000106200010386DB''

'My text file ( total of 3 record )
'********************************************************************************
4834544834565197485837    5197485837TANI08570010KitchWaterON   5199728888     06520000Windsor   ONA00016580000090000000000000000000T070219L  009711020031205104057000120400016626DA

4834544834569054713386    9054713386TANI08810039Markham   ON   2038784667     04550839Milford   CTA00010370000056000000000000000000T070219L  000240120031205104238000106200010386DB

4834544834569052829535    9052829535TANI08230003CooksvilleON41613374430       04500000Switzerlnd  K00001070000007000000000000000000T070219J  008735520031205104256000017800001086DC
'********************************************************************************

i try this code working fine but only where i write the '===' he don't return the good string !!!!

Private Sub cmdFile_Click()
    Dim strTemp As String

    txtfile = ""
    dlg.FileName = "*.txt"
    dlg.ShowOpen
    If Dir(dlg.FileName) <> "" Then
        Open dlg.FileName For Input As 1
        While Not EOF(1)
            Line Input #1, strTemp
            txtfile = txtfile & strTemp & vbCrLf
        Wend
        Close #1
    Else
        MsgBox "File not found"
    End If

    Call FindFirst("PQX01839780010732")
   
End Sub

Function FindFirst(stringLookingfor As String)

    Set Fso = CreateObject("Scripting.FileSystemObject")
    Set fileFso = Fso.CreateTextFile("C:\Documents and Settings\dlaplante\Bureau\temp.log")
           
    If stringLookingfor <> "" Then
        If InStr(txtfile, stringLookingfor) <> 0 Then
           
            'Don't return the good string !!!
            '==============================================
            txtfile.SelStart = InStr(txtfile, stringLookingfor) - 1
            txtfile.SelLength = Len(stringLookingfor)
            '==============================================        
            fileFso.WriteLine (txtfile)
            fileFso.WriteLine ("")
            fileFso.Close
        Else
            MsgBox "Text Not found"
        End If
    End If
End Function
0
Comment
Question by:dany651
5 Comments
 
LVL 14

Accepted Solution

by:
aelatik earned 500 total points
ID: 9912931
Function SearchString(Keyword As String, Filename As String) As String
    SearchString = "Empty"
    Dim MyLine As Variant
    Open Filename For Input As #1
        While Not EOF(1)
            Line Input #1, MyLine
                If InStr(1, LCase(MyLine), LCase(Keyword), vbTextCompare) Then
                    SearchString = MyLine
                End If
        Wend
    Close #1
End Function

Private Sub Form_Load()
    MsgBox SearchString("milford", "c:\install.log")
End Sub
0
 

Expert Comment

by:enari
ID: 9912988
First:
  fileFSO.WriteLine(textfile) will write the whole file, you need
  *** fileFso.WriteLine (txtfile.SelText) ***

Secondly:
The selection items are wrong for selStart and SelLength.
SelStart starts at the location of Milford (at the end of the line)
SelLen = the end of the line.
This code should only return the word "Milford"

You want the whole line Right?

because you put a VBCRLF at the end of the lines, we can search for that:

I would rewrite the function to look like below:

Function FindFirst(stringLookingfor As String)
    Dim i As Integer 'Instr PlaceHolder
    Set Fso = CreateObject("Scripting.FileSystemObject")
    Set fileFso = Fso.CreateTextFile("C:\temp.log")
           
    If stringLookingfor <> "" Then

        i = InStr(txtfile, stringLookingfor) 'Only do this instr once for simplicity
        If i <> 0 Then
           
            'Don't return the good string !!!
            '==============================================
            txtfile.SelStart = InStrRev(txtfile, vbCrLf, i)
            txtfile.SelLength = InStr(i, txtfile, vbCrLf) - txtfile.SelStart
            '==============================================
            fileFso.WriteLine (txtfile.SelText)
            fileFso.WriteLine ("")
            fileFso.Close
        Else
            MsgBox "Text Not found"
        End If
    End If
End Function
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 9913044
aelatik's code should work fine except that the function continues reading the file even after a match is read.  It needs to close the file and exit as soon as the record is found.

Function SearchString(Keyword As String, Filename As String) As String
    SearchString = "Empty"
    Dim MyLine As Variant
    Open Filename For Input As #1
        While Not EOF(1)
            Line Input #1, MyLine
                If InStr(1, LCase(MyLine), LCase(Keyword), vbTextCompare) Then
                    SearchString = MyLine
                    Close #1
                    Exit Function
                End If
        Wend
    Close #1
End Function

Idle_Mind
0
 
LVL 4

Expert Comment

by:MajinLoki
ID: 9913387
An even simpler answer would be to use regular expressions.

Imports regex = System.Web.RegularExpressions

Dim rex As regex.TextRegex                                                     'this will find one (the first) match
Dim rexColl As System.Text.RegularExpressions.MatchCollection  'this can be used to find all instances inside the string

rex = New regex.TextRegex()

if rex.isMatch("Milford") then
    'do whatever
end if

This will be the easiest solution.  Regular expressions also have their own syntax for the strings, so I suggest you look them up in in a search engine.  Hope this helps.
0
 

Author Comment

by:dany651
ID: 9916155
cool i get the answer from aelatik cause i need to make a loop for get all record don't wanna exit when i have foudn a record i wanna continu !!!
the only thing is that all my text files are in a zip file !!!!

if i use a text file the code working fine buit if my text file are in a zipped doesn't work !!!

how he can work wit h a text file in a zip file ???
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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…
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…

876 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