Вы находитесь на странице: 1из 5

Page 1 of 5

This sample program is a demonstration of Record Navigation and Record Searching in Visual Basic 2010. Database being used is Microsoft Access 2010 as back-end.

Database Name: employee.accdb Table Name: tblEmployee PK Field Name Id FName LName Designation Salary Data Type Number Text Text Text Number

Page 2 of 5

Add a Module and write down the code below. 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 tblEmployee ORDER BY Id", Con) Public Dst = New DataSet Public CurrentRow As Integer End Module

Write the codes below in Form1 class. Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load CurrentRow = 0 MsgBox("Provider = " & Con.Provider & " " & "Source = " & Con.DataSource & MsgBoxStyle.OkOnly) Dad.Fill(Dst, "tblEmployee") ShowData(CurrentRow) End Sub Private Sub Id_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles Id.GotFocus Id.SelectAll() End Sub Private Sub ShowData(ByVal CurrentRow) 'Retrieve records and display them in each textbox and label objects Id.Text = Dst.Tables("tblEmployee").Rows(CurrentRow)("Id") FName.Text = Dst.Tables("tblEmployee").Rows(CurrentRow)("FName").ToString.ToUpper LName.Text = Dst.Tables("tblEmployee").Rows(CurrentRow)("LName").ToString.ToUpper Designation.Text = Dst.Tables("tblEmployee").Rows(CurrentRow)("Designation").ToString.ToUpper Salary.Text = Dst.Tables("tblEmployee").Rows(CurrentRow)("Salary")

Page 3 of 5

End Sub Private Sub btnFirst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFirst.Click CurrentRow = 0 ShowData(CurrentRow) End Sub

Private Sub btnPrevious_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrevious.Click If CurrentRow <> 0 Then CurrentRow -= 1 ShowData(CurrentRow) Else MsgBox("First Record is Reached!!!") End If End Sub Private Sub btnLast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLast.Click CurrentRow = Dst.Tables("tblEmployee").Rows.Count - 1 ShowData(CurrentRow) End Sub Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click Dim SearchId As Integer SearchId = Id.Text Dim i, j As Integer j = Dst.Tables("tblEmployee").Rows.Count - 1 i=0 While i <> j + 1 If SearchId = Dst.Tables("tblEmployee").Rows(i)("Id") Then

Page 4 of 5

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 Object, ByVal e As System.EventArgs) Handles Id.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

Page 5 of 5

Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click If CurrentRow <> Dst.Tables("tblEmployee").Rows.Count - 1 Then CurrentRow += 1 ShowData(CurrentRow) Else MsgBox("Last Record is Reached!!!") End If End Sub Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click Application.Exit() End Sub End Class

Вам также может понравиться