-
Dec 10th, 2003, 04:33 PM
#1
Thread Starter
Fanatic Member
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:
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.
Last edited by nkad; Dec 11th, 2003 at 07:54 PM.
-
Jun 7th, 2011, 06:12 AM
#2
New Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|