Search a text file

I am looking for a solution similar to searching ini files for values. I need to translate ID's to a description ithout connecting to a dtabase (that would be too easy). Instead of using a case statement to cover 250 possible combinations, it seems like an approach similar to ini files in VB6 is appropriate. Is there an efficient way to do this in An example of the 2 columns I have to use to search follows. I can export these to a text file with any combination of delimiters, etc that will work.

ID  Description
1   Gas
2   Nitrous Oxide
3   Trinium
4   H2O
5   Silver Nitrate
6   Chromium Sulfate
7   Gold
Who is Participating?
Corey ScheichConnect With a Mentor DeveloperCommented:
Sorry I grabbed an example i had lying around and it was a VB6 example this should get you closer.  It may need a little tweeking.


include system.collections
include system
Include system.IO

Dim MyList as New Collection

Private Sub InitializeMyList()
            Dim SplitLine as arraylist()
            Dim sr As StreamReader = New StreamReader("C:\Temp.txt")
            Dim line As String
            ' Read and display the lines from the file until the end
            ' of the file is reached.
             line = sr.ReadLine()  

                line = sr.ReadLine()
                SplitLine = strline.split(space(1),-1)
                 'the numbers may need adjusting I don't remember if it starts at 1 or 0
                MyList.AddItem Splitline(splitline.count) - 1,Splitline(0)

            Loop Until line Is Nothing
end sub
open the file via a StreamReader, read it all into a string and then search for your phrase by using
the instr-function or the IndexOf-method provided by the string object.

Dim oStream As New System.IO.FileStream("myfile.txt",IO.FileMode.Open)
Dim oReader As New System.IO.StreamReader(oStream)

Dim strText As String= oReader.ReadAll

Dim strPhrase As String="Find this"
If strText.IndexOf(strPhrase,0)=-1 Then
 MsgBox "I found it!"
 MsgBox "Bad luck, not found"
End If
JeepGeekinAuthor Commented:
Thanks for posting this. It seems like a great way to set a flag, but seems like a work around for returning the following value (the description in my case). I guess try to read from the position returned until the end of the line. Is that how you do this? Do you ever use the API calls to read ini files. Sure there was a sub routine you had to create that was less than easy to follow, but you could feed it the ID & it would return the value each time. You could even group the data & tell it which group to go to. Cool stuff. I wonder why they did away with it in

Also, is it good to read in so much data & loop through it like this? I have to do this on 3 or 4 fields all with about 200 - 300 records each on the load event on the form. Sorry. Have to ask.
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

JeepGeekinAuthor Commented:
I'm open to any other suggestions on this one. Anyone have any? Great code, but not sure it's what I'm looking for. Anyone else have a better idea?
Corey ScheichDeveloperCommented:
what about using a Collection
read the file upon initialization

Dim MyList as New Collection

Private Sub InitializeMyList()
    Dim f As Integer
    Dim strLine As String
    Dim ThisText As String
    Dim SplitLine() As String

    f = FreeFile
    Open "C:\Temp.txt" For Input As #f
    'Don't process the first line
    Line Input #f, strLine

    Do Until EOF(f)
        Line Input #f, strLine
        SplitLine = Split(strLine, Space(1), -1, vbTextCompare)

        MyList.AddItem Splitline(Ubound(splitline),Splitline(1)

End Sub

Sub Test ()
'you will have to pass the number you want returned as a string
Msgbox (MyList ("2"))
End Sub

JeepGeekinAuthor Commented:
Looks interesting. I had some trouble with Opening the text file the way you are doing it. Also with "Line Input" & "MyList.AddItem Splitline(Ubound(splitline),Splitline(1)". Do I need to import anything for these to work? So far I am importing System.IO.
JeepGeekinAuthor Commented:
Sorry. I have not worked with external files yet. I'm new to
JeepGeekinAuthor Commented:
OK. I follow this now. I got most of it working (having trouble with the split routine). Until I get this to work I have one question. How does this know to find the ID I am looking for and return the description? I'l look up collections in a bit, but when you are passing it a "2" in your example - does it know to take the "2", find this value in the 1st column (teh ID) and return the value in the second column (the description)? If so, then looks like a great way to handle this. Hope so.
Corey ScheichDeveloperCommented:
I think you got it
A collection is an organized set of objects in this case strings there are 3 ways to parse a collection you can do a For...Each...Next loop or you can get them by their itme number (this isn't suggested because this can change when the collection is edited. or you can select them by their Key.  In the code I took the ID from the file and made it the key of the each object so in your example if you call this line

msgbox (mylist("3"))

it would return a message box containing the word "Trinium"


What sort of problems are you having with split
you may have to declare strline as a string I would have tested the code but I don't have a .Net compiler on my CPU here.

Dim Strline as String
Corey ScheichDeveloperCommented:
(ps if you have a tab delimited file you will have to change the split to use a tab instead of a space)
This is just an idea, why don't you maintain your text file as an ini file data format like -



Nitrous Oxide



Silver Nitrate

Chromium Sulfate


By doing this your can use rich APIs to read, write the data of your text file that has like ini file data format.
JeepGeekinAuthor Commented:
Thanks for the extra info. It looks like this is what I am looking for.
JeepGeekinAuthor Commented:
Also, since you asked, I don't follow the parameters you are sending for the split function (ie: "space(1),-1").  I couldn't get my program to take these.

Also, About the API call. I read that these are no longer available in .Net or they are being phased out.
Corey ScheichDeveloperCommented:
I don't know what you read but I think it is now a function of the string class.

After a bit of reading it looks like the -1 is probably throwing you this error.  Try using 0 or omiting it completely.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.