Hi All,

Wondering if anyone can point me in the right direction.. I have looked at various snippets but nothing seems to be what I need.

I have a tab delimited text file in the form of:

Title: Ms
Forename: Fred
Middle Name Initials:
Surname: Bloggs
Employment Type: Permanent
Employee Number: 9089765
Primary Telephone Number: +44 (131) 456 7890
Secondary Phone Number:
Fax Number: +44 (131) 123 4567
Email Address: your.name@gmail.com
Job Title: business assistant
Company: The City of Anywhere
Department: Children & Families
Division: Directorate
Section: Business Support

I want to read the file and import specific details in to textboxes.

i.e

Forename:
Surname:
Employee Number:

etc, etc

The idea is once I have all the details to export to excel then use a VBScript to create an AD account.

Many thanks inadvance.

For Each line As String In System.IO.File.ReadAllLines("myfile.txt")
    Dim fields() As String = line.Split(vbTab)
Next

After the Split, fields will contain all of the fields in the input line, one field per entry. Forename, Surname and EmployeeNumber are fields(1, 3 and 5) respectively.

Hi,

Managed to get somewhere using a dictonary but now struggling to get data into the textbox.

Private Sub ButRead_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButRead.Click

    Dim sr As New StreamReader("C:\UserDetails.txt")
    Dim openWith As New Dictionary(Of String, String)

    Do While sr.Peek <> -1
        Dim line As String() = sr.ReadLine().Split(":")
        openWith.Add(line(0), line(1))
    Loop

    TxtForename.Text = openWith.ContainsKey("Forename")
    TxtSurname.Text = openWith.ContainsKey("Surname")

End Sub

All I am getting is 'true' in the textboxes but what I need is Fred Bloggs respectivley.

Any help you be greatly appreciated..

Your original post was ambiguous. You said you had a tab delimited file which means one complete record per line with the individual fields separated by a tab character. The form you gave an example of is not tab delimited. I based my answer on option one because I am a programmer and therefore lazy. If your data is in the second form then you sorta do it the same way. If the file contains only one set of data then you can use the following (which will work no matter what order the data is in)

Public Class Form1

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

        Dim record As New Dictionary(Of String, String)

        For Each line As String In System.IO.File.ReadAllLines("d:\temp\test.txt")
            Dim fields() As String = line.Split(":")
            record.Add(Trim(fields(0)), Trim(fields(1)))
        Next

        TextBox1.Text = record("Forename")
        TextBox2.Text = record("Surname")
        TextBox3.Text = record("Employee Number")

    End Sub

End Class

If you have multiple employees in the file then you can create nested dictionaries where the outer dictionary is a

Dim Employees As New Dictionary(Of String, Dictionary(Of String,String))

The key would be something unique such as the Employee Number.

Hey

I was reading this post and also tried you solution Jim (worked like a charm in that case), but I can't get it working in my case which is a bit different.

I have a text file with tab delimited entries like this:

ID Name Surname Address City

9955 Macy Gray Green st. New York

There are multiple entries in this text file of course...

Now my code puts all lines in file into array and the searches this array entries for certain string-like this:

Do
sLine = objReader.ReadLine()
If Not sLine Is Nothing
arrText.Add(sLine)
End If
Loop Until sLine Is Nothing
objReader.Close()

For Each sLine In arrText
If sLine.Contains(IdBox.Text) Then
MsgBox(sLine)
Else
End If
Next

I got so far that "sLine" writes me out "9955 Macy Gray Green st. New York" but I want to split this string as I want to use just name & surname in some textboxes.

Any help would really be appreciated.

Edited 4 Years Ago by razree

Hi

I have tried this but I'm missing something. My code looks like this:

             Do
             sLine = objReader.ReadLine()
                If Not sLine Is Nothing Then
                    arrText.Add(sLine)
                End If

            Loop Until sLine Is Nothing
            objReader.Close()

            For Each sLine In arrText
                If sLine.Contains(IdBox.Text) Then
                    MsgBox(sLine)
                Else
                End If
            Next

            Dim record As New Dictionary(Of String, String)
            Dim fields() As String = sLine.Split(vbTab)
            record.Add(Trim(fields(0)), Trim(fields(3)))

            Main.NameBox.Text = record(sLine)
            Main.Show()

What I want is to take 2nd and 3rd part from string (sline) that I get (ie.

'9955 Macy Gray Green st. New York"

and put it to main form into same text box.

If I run it like this it gives me 'The given key was not present in the dictionary' error.

What am I missing?

Dim lines() As String = System.IO.File.ReadAllLines(myfile)

For Each line As String In Filter(lines,IdBox.Text)
    Dim fields() As String = line.Split(vbTab)
    MsgBox(fields(1) & " " & fields(2))
Next

Filter returns all lines containing the given text.

This question has already been answered. Start a new discussion instead.