Friday, June 10, 2011

module

Option Explicit On
Imports System.Data.OleDb
Imports System.Data
Module Module1
    Public con As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Application.StartupPath & "/db1.mdb; Persist Security Info=False;"
    Public cnSQL As OleDb.OleDbConnection
    Public cmSQL As OleDb.OleDbCommand
    Public strSQL As String
    Public objRead As OleDbDataReader
    Public myDA As OleDbDataAdapter
    Public lv As ListViewItem
    Public ds As DataSet
    Public Sub connection_open()
        cnSQL = New OleDbConnection(con)
        cnSQL.Open()
    End Sub

    Public Sub connection_close()
        cnSQL.Close()
        cnSQL.Dispose()
    End Sub
End Module

codes

Option Explicit On
Imports System.Data.OleDb
Imports System.Data
Public Class Form1


    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        GroupBox1.Enabled = False
        Button3.Enabled = False
        Button5.Enabled = False
        Button4.Enabled = False
        PopulateListView()
        ListView1.Items(0).Selected = True
        ListView1.Select()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        GroupBox1.Enabled = True
        TextBox1.Focus()
        Button1.Enabled = False
        Button4.Enabled = True
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Button1.Enabled = True
        Button4.Enabled = False
        GroupBox1.Enabled = False
        Try
            strSQL = "insert into tbl_stud(name,address) values('" & TextBox1.Text & "','" & TextBox2.Text & "')"
            connection_open()


            cmSQL = New OleDbCommand(strSQL, cnSQL)
            cmSQL.ExecuteNonQuery()
            cmSQL.Dispose()

            connection_close()

        Catch Exp As OleDbException
            MsgBox(Exp.Message, MsgBoxStyle.Critical, "SQL Error")

        Catch Exp As Exception
            MsgBox(Exp.Message, MsgBoxStyle.Critical, "General Error")
        End Try


        MsgBox("Student has been Successfully Added.", MsgBoxStyle.Information)
        PopulateListView()
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Button1.Enabled = False
        Button3.Enabled = True
        Button5.Enabled = True
        Button2.Enabled = False
        GroupBox1.Enabled = True
        TextBox1.Focus()
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Button1.Enabled = True
        Button3.Enabled = False
        Button5.Enabled = False
        Button2.Enabled = True
        GroupBox1.Enabled = False
        Try
            strSQL = "UPDATE tbl_stud SET name='" & TextBox1.Text & "',address='" & TextBox2.Text & "' where id='" & TextBox3.Text & "'"
            connection_open()


            cmSQL = New OleDbCommand(strSQL, cnSQL)
            cmSQL.ExecuteNonQuery()
            cmSQL.Dispose()

            connection_close()

        Catch Exp As OleDbException
            MsgBox(Exp.Message, MsgBoxStyle.Critical, "SQL Error")

        Catch Exp As Exception
            MsgBox(Exp.Message, MsgBoxStyle.Critical, "General Error")
        End Try


        MsgBox("Student has been Successfully Added.", MsgBoxStyle.Information)
        PopulateListView()
    End Sub

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        Button1.Enabled = True
        Button3.Enabled = False
        Button5.Enabled = False
        Button2.Enabled = True
        GroupBox1.Enabled = False
        Try
            strSQL = "delete from tbl_stud where id='" & TextBox3.Text & "'"
            connection_open()


            cmSQL = New OleDbCommand(strSQL, cnSQL)
            cmSQL.ExecuteNonQuery()
            cmSQL.Dispose()

            connection_close()

        Catch Exp As OleDbException
            MsgBox(Exp.Message, MsgBoxStyle.Critical, "SQL Error")

        Catch Exp As Exception
            MsgBox(Exp.Message, MsgBoxStyle.Critical, "General Error")
        End Try


        MsgBox("Student has been Successfully Added.", MsgBoxStyle.Information)
        PopulateListView()
    End Sub
    Private Sub RefreshForm()
        ListView1.Items.Clear()
        Call searchListView()
    End Sub
    Private Sub searchListView()
        connection_open()
        Try
            strSQL = "SELECT * FROM tbl_stud where name like '" & TextBox1.Text & "%'"
            cmSQL = New OleDbCommand(strSQL, cnSQL)
            objRead = cmSQL.ExecuteReader
            While objRead.Read
                lv = Me.ListView1.Items.Add(objRead("id") & "")
                lv.SubItems.Add(objRead("name") & "")
                lv.SubItems.Add(objRead("address") & "")
            End While
            objRead.Close()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        Finally
            connection_close()
        End Try
    End Sub
    Private Sub PopulateListView()
        ListView1.Items.Clear()
        connection_open()
        Try
            strSQL = "SELECT * FROM tbl_stud ORDER BY name Asc"
            cmSQL = New OleDbCommand(strSQL, cnSQL)
            objRead = cmSQL.ExecuteReader
            While objRead.Read
                lv = Me.ListView1.Items.Add(objRead("id") & "")
                lv.SubItems.Add(objRead("name") & "")
                lv.SubItems.Add(objRead("address") & "")
            End While
            objRead.Close()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        Finally
            connection_close()
        End Try
    End Sub

    Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
        If e.KeyChar = Chr(13) Then
            ListView1.Items(0).Selected = True
            ListView1.Select()
        End If
    End Sub

    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
        RefreshForm()
    End Sub

    Private Sub ListView1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListView1.MouseClick
        ListView1_MouseDoubleClick(sender, e)
    End Sub

    Private Sub ListView1_MouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListView1.MouseDoubleClick
        If GroupBox1.Enabled = True Then
            With ListView1.SelectedItems(0)
                TextBox3.Text = .SubItems(0).Text
                TextBox1.Text = .SubItems(1).Text
                TextBox2.Text = .SubItems(2).Text
            End With
        End If
    End Sub
    Private Sub ListView1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles ListView1.KeyPress

        If e.KeyChar = Chr(13) And GroupBox1.Enabled = True Then
            With ListView1.SelectedItems(0)
                TextBox3.Text = .SubItems(0).Text
                TextBox1.Text = .SubItems(1).Text
                TextBox2.Text = .SubItems(2).Text
            End With
        End If

    End Sub
   
   
    Private Sub TextBox3_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox3.TextChanged

    End Sub

    Private Sub ListView1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListView1.SelectedIndexChanged

    End Sub
End Class