Form1.vb

 Imports System.Data.OleDb

Public Class Form1
    Inherits System.Windows.Forms.Form
   

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer. 
    'Do not modify it using the code editor.
    Friend WithEvents GroupBox1 As System.Windows.Forms.GroupBox
    Friend WithEvents GroupBox2 As System.Windows.Forms.GroupBox
    Friend WithEvents DataGrid1 As System.Windows.Forms.DataGrid
    Friend WithEvents Label1 As System.Windows.Forms.Label
    Friend WithEvents Label2 As System.Windows.Forms.Label
    Friend WithEvents Label3 As System.Windows.Forms.Label
    Friend WithEvents Label4 As System.Windows.Forms.Label
    Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
    Friend WithEvents TextBox2 As System.Windows.Forms.TextBox
    Friend WithEvents TextBox3 As System.Windows.Forms.TextBox
    Friend WithEvents TextBox4 As System.Windows.Forms.TextBox
    Friend WithEvents delete As System.Windows.Forms.Button
    Friend WithEvents save As System.Windows.Forms.Button
    Friend WithEvents edit As System.Windows.Forms.Button
    Friend WithEvents ad As System.Windows.Forms.Button
    Friend WithEvents DataGridTableStyle1 As System.Windows.Forms.DataGridTableStyle
    Friend WithEvents DataGridBoolColumn1 As System.Windows.Forms.DataGridBoolColumn
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Dim configurationAppSettings As System.Configuration.AppSettingsReader = New System.Configuration.AppSettingsReader
        Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(Form1))
        Me.GroupBox1 = New System.Windows.Forms.GroupBox
        Me.DataGrid1 = New System.Windows.Forms.DataGrid
        Me.DataGridTableStyle1 = New System.Windows.Forms.DataGridTableStyle
        Me.DataGridBoolColumn1 = New System.Windows.Forms.DataGridBoolColumn
        Me.GroupBox2 = New System.Windows.Forms.GroupBox
        Me.delete = New System.Windows.Forms.Button
        Me.save = New System.Windows.Forms.Button
        Me.edit = New System.Windows.Forms.Button
        Me.ad = New System.Windows.Forms.Button
        Me.TextBox4 = New System.Windows.Forms.TextBox
        Me.TextBox3 = New System.Windows.Forms.TextBox
        Me.TextBox2 = New System.Windows.Forms.TextBox
        Me.TextBox1 = New System.Windows.Forms.TextBox
        Me.Label4 = New System.Windows.Forms.Label
        Me.Label3 = New System.Windows.Forms.Label
        Me.Label2 = New System.Windows.Forms.Label
        Me.Label1 = New System.Windows.Forms.Label
        Me.GroupBox1.SuspendLayout()
        CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.GroupBox2.SuspendLayout()
        Me.SuspendLayout()
        '
        'GroupBox1
        '
        Me.GroupBox1.Controls.Add(Me.DataGrid1)
        Me.GroupBox1.Location = New System.Drawing.Point(0, 0)
        Me.GroupBox1.Name = "GroupBox1"
        Me.GroupBox1.Size = New System.Drawing.Size(304, 168)
        Me.GroupBox1.TabIndex = 0
        Me.GroupBox1.TabStop = False
        Me.GroupBox1.Text = "Employee Details"
        '
        'DataGrid1
        '
        Me.DataGrid1.CaptionBackColor = System.Drawing.Color.PapayaWhip
        Me.DataGrid1.CaptionForeColor = System.Drawing.Color.Black
        Me.DataGrid1.CaptionText = "xyz"
        Me.DataGrid1.DataMember = ""
        Me.DataGrid1.Dock = System.Windows.Forms.DockStyle.Fill
        Me.DataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText
        Me.DataGrid1.Location = New System.Drawing.Point(3, 16)
        Me.DataGrid1.Name = "DataGrid1"
        Me.DataGrid1.PreferredColumnWidth = 64
        Me.DataGrid1.ReadOnly = True
        Me.DataGrid1.RowHeaderWidth = 20
        Me.DataGrid1.Size = New System.Drawing.Size(298, 149)
        Me.DataGrid1.TabIndex = 0
        Me.DataGrid1.TableStyles.AddRange(New System.Windows.Forms.DataGridTableStyle() {Me.DataGridTableStyle1})
        '
        'DataGridTableStyle1
        '
        Me.DataGridTableStyle1.DataGrid = Me.DataGrid1
        Me.DataGridTableStyle1.GridColumnStyles.AddRange(New System.Windows.Forms.DataGridColumnStyle() {Me.DataGridBoolColumn1})
        Me.DataGridTableStyle1.HeaderForeColor = System.Drawing.SystemColors.ControlText
        Me.DataGridTableStyle1.MappingName = ""
        '
        'DataGridBoolColumn1
        '
        Me.DataGridBoolColumn1.FalseValue = False
        Me.DataGridBoolColumn1.HeaderText = CType(configurationAppSettings.GetValue("DataGridBoolColumn1.HeaderText", GetType(System.String)), String)
        Me.DataGridBoolColumn1.MappingName = ""
        Me.DataGridBoolColumn1.NullValue = CType(resources.GetObject("DataGridBoolColumn1.NullValue"), Object)
        Me.DataGridBoolColumn1.TrueValue = True
        Me.DataGridBoolColumn1.Width = 75
        '
        'GroupBox2
        '
        Me.GroupBox2.Controls.Add(Me.delete)
        Me.GroupBox2.Controls.Add(Me.save)
        Me.GroupBox2.Controls.Add(Me.edit)
        Me.GroupBox2.Controls.Add(Me.ad)
        Me.GroupBox2.Controls.Add(Me.TextBox4)
        Me.GroupBox2.Controls.Add(Me.TextBox3)
        Me.GroupBox2.Controls.Add(Me.TextBox2)
        Me.GroupBox2.Controls.Add(Me.TextBox1)
        Me.GroupBox2.Controls.Add(Me.Label4)
        Me.GroupBox2.Controls.Add(Me.Label3)
        Me.GroupBox2.Controls.Add(Me.Label2)
        Me.GroupBox2.Controls.Add(Me.Label1)
        Me.GroupBox2.Location = New System.Drawing.Point(0, 168)
        Me.GroupBox2.Name = "GroupBox2"
        Me.GroupBox2.Size = New System.Drawing.Size(304, 200)
        Me.GroupBox2.TabIndex = 1
        Me.GroupBox2.TabStop = False
        '
        'delete
        '
        Me.delete.BackColor = System.Drawing.Color.PeachPuff
        Me.delete.Location = New System.Drawing.Point(224, 160)
        Me.delete.Name = "delete"
        Me.delete.Size = New System.Drawing.Size(64, 23)
        Me.delete.TabIndex = 11
        Me.delete.Text = "delete"
        '
        'save
        '
        Me.save.BackColor = System.Drawing.Color.PeachPuff
        Me.save.Location = New System.Drawing.Point(152, 160)
        Me.save.Name = "save"
        Me.save.Size = New System.Drawing.Size(64, 23)
        Me.save.TabIndex = 10
        Me.save.Text = "save"
        '
        'edit
        '
        Me.edit.BackColor = System.Drawing.Color.PeachPuff
        Me.edit.Location = New System.Drawing.Point(80, 160)
        Me.edit.Name = "edit"
        Me.edit.Size = New System.Drawing.Size(64, 23)
        Me.edit.TabIndex = 9
        Me.edit.Text = "edit"
        '
        'ad
        '
        Me.ad.BackColor = System.Drawing.Color.PeachPuff
        Me.ad.Location = New System.Drawing.Point(8, 160)
        Me.ad.Name = "ad"
        Me.ad.Size = New System.Drawing.Size(64, 23)
        Me.ad.TabIndex = 8
        Me.ad.Text = "add"
        '
        'TextBox4
        '
        Me.TextBox4.Location = New System.Drawing.Point(104, 120)
        Me.TextBox4.Name = "TextBox4"
        Me.TextBox4.Size = New System.Drawing.Size(168, 20)
        Me.TextBox4.TabIndex = 7
        Me.TextBox4.Text = ""
        '
        'TextBox3
        '
        Me.TextBox3.Location = New System.Drawing.Point(104, 88)
        Me.TextBox3.Name = "TextBox3"
        Me.TextBox3.Size = New System.Drawing.Size(168, 20)
        Me.TextBox3.TabIndex = 6
        Me.TextBox3.Text = ""
        '
        'TextBox2
        '
        Me.TextBox2.Location = New System.Drawing.Point(104, 56)
        Me.TextBox2.Name = "TextBox2"
        Me.TextBox2.Size = New System.Drawing.Size(168, 20)
        Me.TextBox2.TabIndex = 5
        Me.TextBox2.Text = ""
        '
        'TextBox1
        '
        Me.TextBox1.Location = New System.Drawing.Point(104, 24)
        Me.TextBox1.Name = "TextBox1"
        Me.TextBox1.Size = New System.Drawing.Size(168, 20)
        Me.TextBox1.TabIndex = 4
        Me.TextBox1.Text = ""
        '
        'Label4
        '
        Me.Label4.Location = New System.Drawing.Point(16, 120)
        Me.Label4.Name = "Label4"
        Me.Label4.Size = New System.Drawing.Size(88, 23)
        Me.Label4.TabIndex = 3
        Me.Label4.Text = "Location"
        '
        'Label3
        '
        Me.Label3.Location = New System.Drawing.Point(16, 88)
        Me.Label3.Name = "Label3"
        Me.Label3.Size = New System.Drawing.Size(88, 24)
        Me.Label3.TabIndex = 2
        Me.Label3.Text = "Salary"
        '
        'Label2
        '
        Me.Label2.Location = New System.Drawing.Point(16, 56)
        Me.Label2.Name = "Label2"
        Me.Label2.Size = New System.Drawing.Size(88, 23)
        Me.Label2.TabIndex = 1
        Me.Label2.Text = "Employee Name"
        '
        'Label1
        '
        Me.Label1.Location = New System.Drawing.Point(16, 24)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(88, 23)
        Me.Label1.TabIndex = 0
        Me.Label1.Text = "Employee Id"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(304, 373)
        Me.Controls.Add(Me.GroupBox2)
        Me.Controls.Add(Me.GroupBox1)
        Me.Name = "Form1"
        Me.Text = "emp"
        Me.TransparencyKey = System.Drawing.Color.WhiteSmoke
        Me.GroupBox1.ResumeLayout(False)
        CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).EndInit()
        Me.GroupBox2.ResumeLayout(False)
        Me.ResumeLayout(False)

    End Sub

#End Region

#Region "declarations"
    Dim cn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=db1.MDB")
    Dim dad As New OleDbDataAdapter
    Dim ds As New DataSet
    Dim t As DataTable
    Dim rc As Integer
    Dim r As DataRow
#End Region

#Region "form load"

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.Text = "sample application"
        Try
            dad = New OleDbDataAdapter("select * from table1", cn)
            dad.Fill(ds, "emp1")
            t = ds.Tables(0)
            DataGrid1.DataSource = t
            formload()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

#End Region

#Region "textboxes"

    Private Sub TextBox3_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox3.KeyPress
        e.Handled = OnlyNumeric(Asc(e.KeyChar))
    End Sub

    Private Sub TextBox2_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox2.KeyPress
        e.Handled = OnlyCharacter(Asc(e.KeyChar))

    End Sub

    Private Sub TextBox4_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox4.KeyPress
        e.Handled = OnlyCharacter(Asc(e.KeyChar))
    End Sub
#End Region

#Region "buttons"
    Private Sub ad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ad.Click
        Try
            If ad.Text = "add" Then
                TextBox2.Focus()
                TextBox1.Text = Integer.Parse(t.Rows(t.Rows.Count - 1).Item(0)) + 1
                TextBox1.ReadOnly = True
                TextBox2.ReadOnly = False
                TextBox3.ReadOnly = False
                TextBox4.ReadOnly = False
                TextBox2.Clear()
                TextBox3.Clear()
                TextBox4.Clear()
                Me.edit.Enabled = False
                Me.delete.Enabled = False
                Me.save.Enabled = True
                Me.ad.Text = "cancel"
            ElseIf ad.Text = "cancel" Then
                formload()
                Me.ad.Text = "add"

            End If

        Catch ex As Exception
            MessageBox.Show(ex.Message)

        End Try


    End Sub

    Private Sub delete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles delete.Click
        If MessageBox.Show("Do you really want to Delete this Record?", _
        "Delete", MessageBoxButtons.YesNo, _
        MessageBoxIcon.Warning) = DialogResult.No Then


            MsgBox("Operation Cancelled")
            Exit Sub

        End If


        Dim cmd As New OleDbCommandBuilder(dad)
        rc = DataGrid1.CurrentRowIndex
        t.Rows(rc).Delete()
        MsgBox("deleted successfully")
        dad.Update(ds, "emp1")
        formload()
    End Sub




    Private Sub save_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles save.Click
        Dim id As Integer
        Dim str As String
        Dim cmd As New OleDbCommandBuilder(dad)
        str = TextBox1.Text
        id = Integer.Parse(str)
        If id = t.Rows.Count + 1 Then
            r = t.NewRow
            r(0) = TextBox1.Text
            r(1) = TextBox2.Text
            r(2) = TextBox3.Text
            r(3) = TextBox4.Text
            t.Rows.Add(r)
            dad.Update(ds, "emp1")
            MessageBox.Show("added successfully")
            Me.ad.Text = "add"
            Me.ad.Enabled = True
            Me.edit.Enabled = True
            Me.delete.Enabled = True
            Me.save.Enabled = False
        ElseIf id < t.Rows.Count + 1 Then
            r = t.Rows(id - 1)
            r("empname") = TextBox2.Text
            r("salary") = TextBox3.Text
            r("location") = TextBox4.Text
            dad.Update(ds, "emp1")
            Me.save.Enabled = False
        End If


    End Sub

    Private Sub edit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles edit.Click
        r = t.Rows(rc)
        TextBox1.ReadOnly = True
        TextBox2.ReadOnly = False
        TextBox3.ReadOnly = False
        TextBox4.ReadOnly = False
        ad.Text = "cancel"
        Me.save.Enabled = True
        Me.edit.Enabled = False
        ad.Text = "cancel"
        Me.ad.Enabled = True
        Me.delete.Enabled = False

    End Sub
#End Region

#Region "data grid"
    Private Sub DataGrid1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles DataGrid1.MouseUp
        rc = DataGrid1.CurrentRowIndex
        getemp(rc)
        TextBox1.ReadOnly = True
        TextBox2.ReadOnly = False
        TextBox3.ReadOnly = False
        TextBox4.ReadOnly = False
        edit.Enabled = True
        delete.Enabled = True
        ad.Text = "add"
    End Sub

#End Region

#Region "methods"

    Public Sub getemp(ByVal i As Integer)
        r = t.Rows(i)
        TextBox1.Text = r("empid")
        TextBox2.Text = r("empname")
        TextBox3.Text = r("salary")
        TextBox4.Text = r("location")
    End Sub
    Public Sub formload()
        Me.TextBox1.ReadOnly = True
        Me.TextBox2.ReadOnly = True
        Me.TextBox3.ReadOnly = True
        Me.TextBox4.ReadOnly = True
        TextBox1.Clear()
        TextBox2.Clear()
        TextBox3.Clear()
        TextBox4.Clear()
        Me.ad.Enabled = True
        Me.edit.Enabled = False
        Me.save.Enabled = False
        Me.delete.Enabled = False

    End Sub


    Public Function OnlyNumeric(ByVal Key As String) As Boolean
        If (Key >= 48 And Key <= 57) Or Key = 8 Then
            OnlyNumeric = False
        Else
            OnlyNumeric = True
        End If
    End Function


    Public Function OnlyCharacter(ByVal key As String) As Boolean
        If (key >= 65 And key <= 90) Or (key >= 97 And key <= 122) Or key = 8 Then
            OnlyCharacter = False

        Else
            OnlyCharacter = True
            MsgBox("enter only characters")
        End If
    End Function
#End Region

    Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
        If MessageBox.Show("Do you want to terminate the application", "close", MessageBoxButtons.YesNo, MessageBoxIcon.Stop) = DialogResult.No Then
            e.Cancel = True

        End If

      

    End Sub
End Class

Project Homepage: