Dec 10th, 2003, 03:33 PM
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.
Private Sub Command1_Click()
Dim strSearchCriteria As String
Dim strBuffer As String
Dim lngCt As Long
Dim p As Long
strSearchCriteria = Text1.Text
'-Load data from file into strBuffer
Open App.Path & "\test.txt" For Input As #1
strBuffer = Input(LOF(1), 1)
p = 1
'-Find occurances of strSearchCriteria
p = InStr(p, strBuffer, strSearchCriteria, vbTextCompare + vbDatabaseCompare)
If p <> 0 Then
'-We found one!
lngCt = lngCt + 1
'-No more left, display results.
If lngCt = 0 Then
MsgBox "'" & strSearchCriteria & "' Not Found.", vbInformation + okonly
MsgBox "'" & strSearchCriteria & "' Was found " & lngCt & " times.", vbInformation + okonly
p = p + Len(strSearchCriteria) '-This keeps the current position in strBuffer
Hope you like.
Last edited by nkad; Dec 11th, 2003 at 06:54 PM.
Jun 7th, 2011, 06:12 AM
Re: VB - Search Text In File
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
Click Here to Expand Forum to Full Width