VB - Search Text In File-VBForums
Results 1 to 2 of 2

Thread: VB - Search Text In File

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2000
    Posts
    770

    VB - Search Text In File

    Description: Searches for a string phrase, such as "red sports car" within a text or data file and returns how many occurrences of the string there where.

    Requirements: Add a Command Button and Text Box to your form.

    Notes: This search function is case-insensitive. Strings such as "Red Sports Car" and "rED SpOrTs car", mean the same. Text1.Text is the string value to be serached.


    VB Code:
    1. Private Sub Command1_Click()
    2.     Dim strSearchCriteria As String
    3.     Dim strBuffer As String
    4.     Dim lngCt As Long
    5.     Dim p As Long
    6.    
    7.     strSearchCriteria = Text1.Text
    8.    
    9.     '-Load data from file into strBuffer
    10.     Open App.Path & "\test.txt" For Input As #1
    11.         strBuffer = Input(LOF(1), 1)
    12.     Close #1
    13.    
    14.     '-Search strBuffer
    15.     p = 1
    16.  
    17.     Do
    18.         '-Find occurances of strSearchCriteria
    19.         p = InStr(p, strBuffer, strSearchCriteria, vbTextCompare + vbDatabaseCompare)
    20.         If p <> 0 Then
    21.             '-We found one!
    22.             lngCt = lngCt + 1
    23.         Else
    24.             '-No more left, display results.
    25.             If lngCt = 0 Then
    26.                 MsgBox "'" & strSearchCriteria & "' Not Found.", vbInformation + okonly
    27.             Else
    28.                 MsgBox "'" & strSearchCriteria & "' Was found " & lngCt & " times.", vbInformation + okonly
    29.             End If
    30.             Exit Do
    31.         End If
    32.        
    33.         p = p + Len(strSearchCriteria) '-This keeps the current position in strBuffer
    34.     Loop
    35. End Sub


    Hope you like.
    Last edited by nkad; Dec 11th, 2003 at 07:54 PM.

  2. #2
    New Member
    Join Date
    Jun 2011
    Posts
    1

    Re: VB - Search Text In File

    hi
    im using vb6 and im very new to vb. i want to search string values in a text file. i could able to search single value. my string variable name is coms where the values are "PIO # 3 , PIO # 4 , PIO #6......" like this but all in one by one. i dont know how to search that string values in text file. If anyone could help me means i ll feel easy to finish my project.

    Thanks in Advance

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.