My program opens a file as a string, and I need to know how to use a text box to find a word in the string, and then display all the instances it finds to a listbox.
Can anyone help me out here?
Ok, I'm not searching for files, I'm trying to find text in a file... Now I used to know how to do this, but it's been 3 years since I've messed around with this stuff... Here is what I got for Open:
wrap$ = Chr$(13) + Chr$(10)
CommonDialog1.Filter = "Text files (*.TXT) | *.TXT"
CommonDialog1.ShowOpen
If CommonDialog1.FileName <> "" Then
Open CommonDialog1.FileName For Input As #1
Do Until EOF(1)
Line Input #1, alltext$
file$ = file$ + alltext$ + wrap$
Loop
Close #1
MsgBox ("Your File Has Been Loaded!", vbInformation, "NOTAMS"
Else
Exit Sub
End If
I have a menu item that brings up an inputbox and there the user selects what they want to view(between 3 things POC, compname ,and notam). In my add I concatenate all 3 of these things into one line of text which saves into the file they have opened in the above code.
After the user enters their choice, I want the results to show up in a listbox. I know there is a way to sort through a string, but I can't remember if I have to create some sort of array or not(that's where the 3 years hits me)
If anyone could help me out, it would be very much appreciated!
Why do you use the lines
Do Until EOF(1)
Line Input #1, alltext$
file$ = file$ + alltext$ + wrap$
Loop
You should use the textstring readall command ie text$ = file.readall
This will make one string for the whole file. The user then wants to search for a particular string you can then split the string into however many lines, using the same array each time. When the loop finds the string you are looking for, it will then post the instance into the listview box.
Hey thanks a lot!!
BiggerBrother the reason I use those lines is because the last time I had to write a progam, I was using visual basic 2.0, and I'm not real familiar with all the new stuff in 6.0 . I have basically been using trial and error to see if the stuff that I used to know works in 6. Most of the code still works, even though it may seem kind of old and ridiculous.
Once again thanks for your help!
Sub form_load()
myString = inputbox("Please enter a string to search for:"
if mystring <>"" then
Call FindObject
else
exit sub
end if
end sub
Sub FindObject()
dim newFSO as new filesystemobject
dim ts as textstream
dim entry as string
dim var(3) as string 'variable to assign your lines to.
dim i as integer ' count
set ts = newFSO.OpenTextFile("myTextFile.txt", ForReading)
Do
entry = ts.readline
var() = split(entry, "," ' split the returned string into 3, separated by in this case a comma.
for n=1 to 3
if var=mystring then
myListBox.additem Var(1) & " " & Var(2) & " " Var(3)
end if
next n
loop until ts.atendofstream
ts.close
if myListBox.ListCount = 0 then
msgBox "Sorry, But no matches for your entry were found"
END IF
end sub
Hope this helps. I hope you have used delimiters, otherwise, you can search the string. Try this:
Sub FindObject()
dim newFSO as new filesystemobject
dim ts as textstream
dim entry as string
dim var(3) as string 'variable to assign your lines to.
dim i as integer ' count
set ts = newFSO.OpenTextFile("myTextFile.txt", ForReading)
Do
entry = ts.readline
if instr(1,entry,mystring) = true then
myListBox.additem entry
end if
loop until ts.atendofstream
ts.close
if myListBox.ListCount = 0 then
msgBox "Sorry, But no matches for your entry were found"
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.