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:
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)
Close #1
'-Search strBuffer
p = 1
Do
'-Find occurances of strSearchCriteria
p = InStr(p, strBuffer, strSearchCriteria, vbTextCompare + vbDatabaseCompare)
If p <> 0 Then
'-We found one!
lngCt = lngCt + 1
Else
'-No more left, display results.
If lngCt = 0 Then
MsgBox "'" & strSearchCriteria & "' Not Found.", vbInformation + okonly
Else
MsgBox "'" & strSearchCriteria & "' Was found " & lngCt & " times.", vbInformation + okonly
End If
Exit Do
End If
p = p + Len(strSearchCriteria) '-This keeps the current position in strBuffer
Loop
End Sub
Hope you like.