Record Searching & Navigation in Vb.net

This article is about "how to search and navigate records in vb.net". In the given example front end to back end connectivity is described and it is very easy to understand & learn.
  • 12913
 

This example helps you to learn how to deal with database in order to search & navigate the records using

  • VB.Net as Front End

  • Microsoft Office Access 2007 as Back End

Database Name : employee.accdb

Table Name : empinfo

No. of fields in table :

  1. Employee Id as Id

  2. First Name as FName

  3. Last Name as LName

  4. Designation as Designation

  5. Salary as Salary

Code:

  • Module1

Imports System.Data.OleDb

Module Module1

    Public Con As OleDbConnection = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0; Data Source=.\\employee.accdb")

    Public Dad As OleDbDataAdapter = New OleDbDataAdapter("SELECT * FROM empinfo ORDER BY Id", Con)

    Public Dst = New DataSet

    Public CurrentRow As Integer

End Module

  • Form1

Imports System.Data.OleDb

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.ObjectByVal e As System.EventArgsHandlesMyBase.Load

        CurrentRow = 0

        'MsgBox("Provider = " & Con.Provider & " " & "Source = " & Con.DataSource & MsgBoxStyle.OkOnly)

        Dad.Fill(Dst, "employee")

        ShowData(CurrentRow)

    End Sub

    Private Sub Button1_Click(ByVal sender As System.ObjectByVal e As System.EventArgs)Handles ExitNow.Click

        Me.Close()

    End Sub

 

    Private Sub Id_GotFocus(ByVal sender As ObjectByVal e As System.EventArgsHandlesId.GotFocus

        Id.SelectAll()

    End Sub

 

    Private Sub ShowData(ByVal CurrentRow)

        Id.Text = Dst.Tables("employee").Rows(CurrentRow)("Id")

        FName.Text = Dst.Tables("employee").Rows(CurrentRow)("FName").ToString.ToUpper

        LName.Text = Dst.Tables("employee").Rows(CurrentRow)("LName").ToString.ToUpper

        Designation.Text = Dst.Tables("employee").Rows(CurrentRow)("Designation").ToString.ToUpper

        Salary.Text = Dst.Tables("employee").Rows(CurrentRow)("Salary").ToString.ToUpper

    End Sub

    Private Sub First_Click(ByVal sender As System.ObjectByVal e As System.EventArgsHandlesFirst.Click

        CurrentRow = 0

        ShowData(CurrentRow)

    End Sub

    Private Sub Forward_Click(ByVal sender As System.ObjectByVal e As System.EventArgs)Handles Forward.Click

        If CurrentRow <> Dst.Tables("employee").Rows.Count - 1 Then

            CurrentRow += 1

            ShowData(CurrentRow)

        Else

            MsgBox("Last Record is Reached!!!")

        End If

    End Sub

 

    Private Sub Previous_Click(ByVal sender As System.ObjectByVal e As System.EventArgs)Handles Previous.Click

        If CurrentRow <> 0 Then

            CurrentRow -= 1

            ShowData(CurrentRow)

        Else
            MsgBox("First Record is Reached!!!")

        End If

    End Sub

    Private Sub Last_Click(ByVal sender As System.ObjectByVal e As System.EventArgsHandlesLast.Click

        CurrentRow = Dst.Tables("employee").Rows.Count - 1

        ShowData(CurrentRow)

    End Sub

    Private Sub Search_Click(ByVal sender As System.ObjectByVal e As System.EventArgs)Handles Search.Click

        Dim SearchId As Integer

        SearchId = Id.Text

        Dim i, j As Integer

 

        j = Dst.Tables("employee").Rows.Count - 1

        i = 0

        While i <> j + 1

            If SearchId = Dst.Tables("employee").Rows(i)("Id"Then

                ShowData(i)

                Exit While

            ElseIf i = j Then

                Clear()

                MsgBox("Record Not Found!!!")

                i = 0

                ShowData(i)

                Exit While

            End If

            i += 1

        End While

        CurrentRow = i

    End Sub

    Private Sub Clear()

        Id.Text = ""

        FName.Text = ""

        LName.Text = ""

        Designation.Text = ""

        Salary.Text = ""

    End Sub

 

    Private Sub Id_LostFocus(ByVal sender As ObjectByVal e As System.EventArgsHandlesId.LostFocus

        Dim i As Integer
        If Id.Text = "" Or IsNothing(Id.Text) = True Or IsNumeric(Id.Text) = False Then

            Clear()

            MsgBox("Integer Value Required!!!")

            i = 0
            ShowData(i)

            CurrentRow = i

        End If

     End Sub

End Class

Output:

View of Form at Starting

picture1.gif

When you are at first record & try to see previous record a message will pop-up as shown in below figure

picture2.gif

When you are at last record & try to see next record a message will pop-up as shown in below figure

 picture3.gif

The error (shown in below figure) will encountered if you insert the value apart from integer in "Id" field to search the records

picture4.gif

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.