READ TEXT FILE AND RETURN A RECORD VB

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
dany651Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

aelatikCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
enariCommented:
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
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
MajinLokiCommented:
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
dany651Author Commented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.