1.11M Members

Reading Sequential Access Files

 
0
 

Hi All,

I have just started building a program that will allow a user to select a plant from a dropdown list and then click a button to view detail related to the selected plant in another form. I would like to be able to read from a sequential access file and instead of reading the total record into one textbox or label, have parts of the file read in different textboxes or labels. For example, plant description in one box/label, plant light needs in another, etc.

Is what I want to do possible?

 
0
 

It's certainly possible. How you do it depends on a few things. For example

  1. Is each plant description in a separate file?
  2. How are the fields identified

My preference is to read files all at once. This avoids the cumbersome process of setting up stream readers, opening, closing, etc. Let's make a few assumptions

  1. the plant info is in a separate file for each plant (where the file is named as plantname.txt)
  2. the folder containing the plant info files is stored in PLANTINFO
  3. the name of the selected plant has been copied to a string variable, plant

then reading all of the info for a plant is as simple as

Dim infofile As String = System.IO.Path.Combine(PLANTINFO, plant & ".txt")

If My.Computer.FileSystem.FileExists(infofile) Then
    Dim info() As String = System.IO.File.ReadAllLines(infofile)
Else
    MsgBox("No file found for " & plant)
    Exit Sub
End If

Now, of course, you have to go through the various lines just read and get the plant properties. This depends on how the file is arranged. For example, if the lines look like

DESCRIPTION: some text
LIGHT:       some more text
ZONE:        still more text
HEIGHT:      yet more text

Then you can parse out the fields by

For Each line As String In info
    If Trim(line) <> "" Then
        Dim fields() As String = line.Split(":")
        Select Case UCase(fields(0))
            Case "DESCRIPTION"
                'copy fields(1) to textbox
            Case "LIGHT"
                'copy fields(1) to textbox
            Case "ZONE"
                'copy fields(1) to textbox
            Case "HEIGHT"                    
                'copy fields(1) to textbox
            Case Else
                MsgBox("Unknown field " & fields(0))
        End Select
    End If
Next

If you have all of the plant info in one file then you have to scan for the correct plant (more code) and maintaining the info becomes more difficult.

 
0
 

Thank you so much Rev. (By the way, I am guessing you have gardening knowledge as well - hence the "ZONE" reference. Cool)

I have followed your outline rather closely. Unfortunately, I am getting the MsgBox unknown field error whenever I select an item from the list and am unsure why. Should I list my lstPlants items differently?

Private Sub lstPlants_SelectedValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstPlants.SelectedValueChanged

        Dim plant As String

        Dim infofile As String

        lstPlants.Items.Add("Bee Balm")
        lstPlants.Items.Add("Black Eyed Susan")
        lstPlants.Items.Add("Bleeding Heart")
        lstPlants.Items.Add("Garden Phlox")
        lstPlants.Items.Add("Maltese Cross")
        lstPlants.Items.Add("Purple Coneflower")
        lstPlants.Items.Add("Siberian Iris")
        lstPlants.Items.Add("Strawberry Foxglove")
        lstPlants.Items.Add("Sunflower")

        plant = lstPlants.SelectedItem

        infofile = plant & ".txt"

        'Dim infofile As String = System.IO.Path.Combine(plant & ".txt")

        If My.Computer.FileSystem.FileExists(infofile) Then
            Dim info() As String = System.IO.File.ReadAllLines(infofile)

        End If

        For Each line As String In infofile
            If Trim(line) <> "" Then
                Dim fields() As String = line.Split(":")
                Select Case UCase(fields(0))
                    Case "COMMON"
                        lblCommonName.Text = fields(1)
                    Case "LATIN"
                        lblLatinName.Text = fields(1)
                    Case "DESCRIPTION"
                        lblDescription.Text = fields(1)
                    Case "COLOR"
                        lblColor.Text = fields(1)
                    Case "HEIGHT"
                        lblHeight.Text = fields(1)
                    Case "WIDTH"
                        lblWidth.Text = fields(1)
                    Case "ZONE"
                        lblZone.Text = fields(1)
                    Case "MOISTURE"
                        lblMoisture.Text = fields(1)
                    Case "LIGHT"
                        lblSun.Text = fields(1)
                    Case "CULTIVATION"
                        lblCultivate.Text = fields(1)
                    Case "PROPAGATION"
                        lblPropagation.Text = fields(1)
                    Case "DISEASE"
                        lblDiseases.Text = fields(1)
                    Case Else
                        MsgBox("Unknown field " & fields(0))
                End Select
            End If
        Next


    End Sub
 
0
 

Can you please post a sample plant info file?

 
0
 

The bee balm file is attached. Thanks again!

 
0
 

Hmmm, didn't see there. Trying again.

 
0
 

Here is the text of the bee balm.txt file:

COMMON: Bee Balm
LATIN: Monarda didyma
DESCRIPTION: Bee balm is a lovely perennial with bright flowers on sturdy stems that grow from fast creeping runners. The pointed oval leaves give Earl Grey tea its distinctive aroma and flavor.
COLOR: Tight heads of tubular red flowers are surrounded by a whorl of colored leafy bracts (modified leaves).
FLOWERING: Summer
HEIGHT: 2-4 feet
SPREAD: 2-3 feet
TEMP:
ZONE: 4-8
MOISTURE: Evenly moist, humus-rich soil. If plants dry out, the lower foliage will be shed.
LIGHT: Full sun to partial shade.
CULTIVATION: Plants spread quickly; divide every two to three years.
PROPAGATION: Divide in spring or fall. Sow seed indoors or outdoors in spring.
DISEASE: Powdery mildew causes whiete blotches on the foliage and may cover the entire plant. Thin the stems for good air circulation. Cut affected plants to the ground.
LANDSCAPE: Plant in formal or informal gardens. Bee balm's lovely flowers add brilliant color to the summer garden and are favored by hummingbirds.

 
2
 

I tried the following code and it worked fine. Note that I didn't bother filling in complete code for all fields. This should be enough to give you the idea.

If My.Computer.FileSystem.FileExists(infofile) Then

    For Each line As String In System.IO.File.ReadAllLines(infofile)

        If Trim(line) <> "" Then

            Dim fields() As String = line.Split(":")

            Select Case UCase(fields(0))
                Case "DESCRIPTION"
                Case "FLOWERING"
                Case "HEIGHT"
                Case "SPREAD"
                Case "TEMP"
                Case "ZONE"
                Case "MOISTURE"
                Case "LIGHT"
                Case "CULTIVATION"
                Case "PROPAGATION"
                Case "DISEASE"
                Case "LANDSCAPE"
                Case "COMMON"
                    Debug.WriteLine("COMMON NAME = " & Trim(fields(1)))
                Case "LATIN"
                    Debug.WriteLine("LATIN NAME  = " & Trim(fields(1)))
                Case "COLOR"
                    Debug.WriteLine("COLOR       = " & Trim(fields(1)))
                Case Else
                    MsgBox("unknown field " & fields(0))
            End Select

        End If

    Next

End If
 
0
 

You are awesome!! Thank you sooo much for your help.

Question Answered as of 1 Year Ago by Reverend Jim
 
0
 

<blush> aw shucks.

You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article