I need to implement a search function button to my project where you click an input button which asks you to input a ID number then displays the details of that record in a form. Thanks.

I am saving the data in a sequential dat file thanks.

Imports System.IO

Public Class Form1

    Public input As FileStream
    Public output As FileStream
    Public fileReader As StreamReader
    Public fileWriter As StreamWriter
    Public fileName As String = "list.dat"
    Public inputfield() As String
    Public fuelsarray() = IO.File.ReadAllLines(fileName)
    Public I As Integer
    Public d As Integer = 0

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpenData.Click


        Dim fileChooser As New OpenFileDialog()
        Dim result As DialogResult = fileChooser.ShowDialog()
        Dim fileName As String

        If result = Windows.Forms.DialogResult.Cancel Then
            Return
        End If

        fileName = fileChooser.FileName

        If fileName = "" Or fileName Is Nothing Then
            MessageBox.Show("Invalid File Name", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Else
            input = New FileStream(fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite)
            fileReader = New StreamReader(input)

        End If

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click


        fileReader.Close()
        input.Close()

        Dim fuelsarray() = IO.File.ReadAllLines(fileName)

        If I > fuelsarray.GetUpperBound(0) Then
            MsgBox("You are at the end of the file can't go beyond!", MsgBoxStyle.Information)
            I = fuelsarray.GetUpperBound(0)
            Exit Sub

        End If

        Dim inputRecord As String

        inputRecord = fuelsarray(I)

        inputfield = inputRecord.Split(","c)

        txtIDNumber.Text = inputfield(0)
        txtLastName.Text = inputfield(1)
        txtFirstName.Text = inputfield(2)
        txtStreetAddress.Text = inputfield(3)
        txtSuburb.Text = inputfield(4)
        txtCity.Text = inputfield(5)
        txtState.Text = inputfield(6)
        txtMember.Text = inputfield(7)
        d = I
        I = I + 1


    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddNew.Click


        fileReader.Close()
        input.Close()

        output = New FileStream(fileName, FileMode.Append, FileAccess.Write)
        fileWriter = New StreamWriter(output)
        fileWriter.WriteLine(txtIDNumber.Text & "," & txtLastName.Text & "," & txtFirstName.Text & "," & txtStreetAddress.Text & "," & txtSuburb.Text & "," & txtCity.Text & "," & txtState.Text & "," & txtMember.Text)

        fileWriter.Close()
        output.Close()

        txtIDNumber.Text = ""
        txtLastName.Text = ""
        txtFirstName.Text = ""
        txtStreetAddress.Text = ""
        txtSuburb.Text = ""
        txtCity.Text = ""
        txtState.Text = ""
        txtMember.Text = ""
        MsgBox("Record added!", MsgBoxStyle.Information)

        txtIDNumber.Focus()

    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSaveChange.Click


        fileReader.Close()
        input.Close()

        Dim fuelsarray() = IO.File.ReadAllLines(fileName)

        Dim objStreamWriter As StreamWriter

        objStreamWriter = New StreamWriter(fileName, False)
        fuelsarray(I) = txtIDNumber.Text & "," & txtLastName.Text & "," & txtFirstName.Text & "," & txtStreetAddress.Text & "," & txtSuburb.Text & "," & txtCity.Text & "," & txtState.Text & "," & txtMember.Text

        For t As Integer = 0 To fuelsarray.GetUpperBound(0)
            objStreamWriter.WriteLine(fuelsarray(t))
        Next



        objStreamWriter.Close()

    End Sub

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click


        fileReader.Close()
        input.Close()

        Dim fuelsarray() = IO.File.ReadAllLines(fileName)

        Dim objStreamWriter As StreamWriter

        objStreamWriter = New StreamWriter(fileName, False)
        For t As Integer = d To fuelsarray.GetUpperBound(0) - 1
            fuelsarray(t) = fuelsarray(t + 1)
        Next


        For t As Integer = 0 To fuelsarray.GetUpperBound(0) - 1
            objStreamWriter.WriteLine(fuelsarray(t))
        Next

        objStreamWriter.Close()


    End Sub

    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrevious.Click


        Dim fuelsarray() = IO.File.ReadAllLines(fileName)
        fileReader.Close()
        input.Close()

        Dim inputRecord As String

        If I < 0 Then
            MsgBox("You are at the first record can't go beyond the first record!", MsgBoxStyle.Information)
            I = 0
            Exit Sub

        End If

        inputRecord = fuelsarray(I)

        inputfield = inputRecord.Split(","c)

        txtIDNumber.Text = inputfield(0)
        txtLastName.Text = inputfield(1)
        txtFirstName.Text = inputfield(2)
        txtStreetAddress.Text = inputfield(3)
        txtSuburb.Text = inputfield(4)
        txtCity.Text = inputfield(5)
        txtState.Text = inputfield(6)
        txtMember.Text = inputfield(7)
        d = I
        I = I - 1



    End Sub

    Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplayRecords.Click


        Dim fileName As String = "list.dat"
        Dim filereader As New IO.StreamReader(fileName)

        ListBox1.Items.AddRange(Split(filereader.ReadToEnd, _
            vbCrLf))
        ListBox1.SelectedIndex = 0
        filereader.Close()




    End Sub

    Private Sub Button11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button11.Click
       

    End Sub

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

    End Sub
End Class
This article has been dead for over six months. Start a new discussion instead.