WHAT'S NEW?
Loading...

How to add, delete, update records in Visual Basic


I used unbound approach to connect my database where you may able add,delete,update and load all records from database. I used OLEDBConnection,OLEDBDataAdapter, and OLEDBCommandBuilder to process all transaction, any way this is an old approach in Visual  Basic. Hopefully this is very helpful to everyone!

Good Luck.....

Here are the Sample Codes:

'-----------------This is Form1----------------------

'----------------------------------------------------

Public Class Form1

    'Public variables
    Dim max As Integer
    Dim inc As Integer

    Dim con As New OleDb.OleDbConnection
    Dim dbprovider As String
    Dim dbsource As String
    Dim ds As New DataSet
    Dim da As OleDb.OleDbDataAdapter
    Dim sql As String
    Dim binding As BindingSource

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

        btnadd.Focus()
        txtlastname.Enabled = False
        txtfirstname.Enabled = False
        txtmiddlename.Enabled = False
        btnsave.Enabled = False
        btnupdate.Enabled = False
        'you can used method name to call your connection everytime you connect your database
        '-------Private Sub myconnection()
        '-------con = New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:/data/dbinfo.mdb;")
        '-------con.Open()
        dbprovider = "PROVIDER = Microsoft.Jet.OLEDB.4.0;"
        dbsource = "Data Source = ../Data/Info.mdb"
        con.ConnectionString = dbprovider & dbsource
        con.Open()

        sql = "SELECT * FROM tblinfo"
        da = New OleDb.OleDbDataAdapter(sql, con)
        da.Fill(ds, "Info")

        txtlastname.Text = ds.Tables("Info").Rows(inc).Item("Lastname")
        txtfirstname.Text = ds.Tables("Info").Rows(inc).Item("Firstname")
        txtmiddlename.Text = ds.Tables("Info").Rows(inc).Item("Middlename")
        txtID.Text = ds.Tables("Info").Rows(inc).Item("ID")
        max = ds.Tables("Info").Rows.Count
        inc = -1

    End Sub
    Private Sub NavigateRecords() 'navigate records from my database

        txtlastname.Text = ds.Tables("Info").Rows(inc).Item("Lastname")
        txtfirstname.Text = ds.Tables("Info").Rows(inc).Item("Firstname")
        txtmiddlename.Text = ds.Tables("Info").Rows(inc).Item("Middlename")
        txtID.Text = ds.Tables("Info").Rows(inc).Item("ID")

    End Sub

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

        If inc <> max - 1 Then
            inc = inc + 1
            NavigateRecords() 'this is where i call my method name to navigate records
        Else
            MsgBox("No Records Yet")
        End If

    End Sub
    Private Sub btnprevious_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnprevious.Click

        If inc > 0 Then
            inc = inc - 1
            NavigateRecords() 'this is where i call my method name to navigate records
        ElseIf inc = -1 Then
            MsgBox("No Records Yet")
        ElseIf inc = 0 Then
            MsgBox("First Row")
        End If

    End Sub
    Private Sub btnlast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnlast.Click

        If inc <> max - 1 Then
            inc = max - 1
            NavigateRecords() 'this is where i call my method name to navigate records
        ElseIf MsgBox("No more records") Then
            MsgBox("Last Record")
        End If

    End Sub
    Private Sub btnfirst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnfirst.Click

        If inc <> 0 Then
            inc = 0
            NavigateRecords() 'this is where i call my method name to navigate records
        ElseIf MsgBox("No more records") Then
            MsgBox("You are in the first record")
        End If

    End Sub
    Private Sub btnadd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnadd.Click

        'add new records to clear all textbox in order to add new entery
        'use enabled true/false to handled process

        txtlastname.Enabled = True
        txtfirstname.Enabled = True
        txtmiddlename.Enabled = True
        txtlastname.Clear()
        txtfirstname.Clear()
        txtmiddlename.Clear()
        txtlastname.Focus()

        btnedit.Enabled = False
        btndelete.Enabled = False
        btnsave.Enabled = True
        btnadd.Enabled = False

    End Sub
    Private Sub btnsave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsave.Click

        'save command
        'you can used this command:
        '-----------sql = "INSERT INTO tblinfo(Firstname,Lastname) VALUES('" & txtfirstname.Text & "','" & txtlastname.Text & "')"
        '-----------Command = New OleDb.OleDbCommand(sql, con)
        '-----------count = Command.ExecuteNonQuery
        '-----------MessageBox.Show(count & " record added to your database")

        Dim cb As New OleDb.OleDbCommandBuilder(da)
        Dim dsnewrow As DataRow

        If txtfirstname.Text = "" Or txtlastname.Text = "" Then
            MsgBox("Please fill up the textbox to continue your transaction", MsgBoxStyle.Exclamation, "Fill the textboxes")
            txtlastname.Focus()
            Exit Sub

        End If
        dsnewrow = ds.Tables("Info").NewRow()
        dsnewrow.Item("Lastname") = txtlastname.Text
        dsnewrow.Item("Firstname") = txtfirstname.Text
        dsnewrow.Item("Middlename") = txtmiddlename.Text
        ds.Tables("Info").Rows.Add(dsnewrow)

        da.Update(ds, "Info")

        MsgBox("New Record Added to your database", MsgBoxStyle.Information)

        If MsgBox("Do you want another transaction to Save?", MsgBoxStyle.YesNo + MsgBoxStyle.Question) = MsgBoxResult.No Then

            btnadd.Enabled = True
            btnsave.Enabled = False
            txtfirstname.Clear()
            txtlastname.Clear()
            txtmiddlename.Clear()

            Exit Sub
        Else
            txtfirstname.Clear()
            txtlastname.Clear()
            txtmiddlename.Clear()
            btnadd.Enabled = False
            btnsave.Enabled = True
            btnedit.Enabled = True
            btndelete.Enabled = True

        End If
    End Sub
    Private Sub btnedit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnedit.Click

        'enabled true to change/modify your records
        txtlastname.Enabled = True
        txtfirstname.Enabled = True
        txtmiddlename.Enabled = True
        If txtlastname.Text = "" Or txtfirstname.Text = "" Then

            MsgBox("You could not edit because the textbox is empty", MsgBoxStyle.Exclamation)

            btnupdate.Enabled = False
            btndelete.Enabled = True
            txtlastname.Enabled = False
            txtfirstname.Enabled = False
            txtmiddlename.Enabled = False

        Else

            txtlastname.Focus()
            btnupdate.Enabled = True
            btnedit.Enabled = False
            btnadd.Enabled = False
            btndelete.Enabled = False

        End If
    End Sub
    Private Sub btnupdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnupdate.Click

        'this line of code enabled you to update the record(s)

        Dim cb As New OleDb.OleDbCommandBuilder(da)

        ds.Tables("Info").Rows(inc).Item("Lastname") = txtlastname.Text
        ds.Tables("Info").Rows(inc).Item("Firstname") = txtfirstname.Text
        ds.Tables("Info").Rows(inc).Item("Middlename") = txtmiddlename.Text

        da.Update(ds, "Info")

        MsgBox("New Record(s)updated", MsgBoxStyle.Information)

        btnedit.Enabled = True
        btnupdate.Enabled = False
        btnadd.Enabled = True

    End Sub

    Private Sub btndelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btndelete.Click

        'this line of code to delete the records from your database

        txtlastname.Enabled = True
        txtfirstname.Enabled = True
        txtmiddlename.Enabled = True
        btnedit.Enabled = False
        btnadd.Enabled = False

        Dim cb As New OleDb.OleDbCommandBuilder(da)

        If txtfirstname.Text = "" Or txtmiddlename.Text = "" Then
            MsgBox("Could not delete because the textbox is empty, Please navigate the record to find what you are going to delete", MsgBoxStyle.Critical)

            btnedit.Enabled = True
            btnadd.Enabled = True
            txtlastname.Enabled = False
            txtfirstname.Enabled = False
            txtmiddlename.Enabled = False

            Exit Sub
        End If

        If MsgBox("Do you really want to delete this record?", MsgBoxStyle.YesNo + MsgBoxStyle.Question) = MsgBoxResult.No Then

            MsgBox("Cancelled Opereation", MsgBoxStyle.Information)

            btnedit.Enabled = True
            btnadd.Enabled = True
            txtlastname.Enabled = False
            txtfirstname.Enabled = False
            txtmiddlename.Enabled = False

            Exit Sub

        End If

        If txtfirstname.Text = "" Or txtmiddlename.Text = "" Then
            MsgBox("Could not delete because the textbox is empty, Please navigate the record to find what you are going to delete", MsgBoxStyle.Critical)

            btnedit.Enabled = True
            btnadd.Enabled = True
        Else
            ds.Tables("Info").Rows(inc).Delete()

            max = max - 1

            inc = 0

            NavigateRecords()

            da.Update(ds, "Info")
            MsgBox("Record(s) deleted", MsgBoxStyle.Information)

            btnedit.Enabled = True
            btnadd.Enabled = True

        End If

    End Sub

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click

        If MsgBox("Do you want to exit the system?", MsgBoxStyle.YesNo + MsgBoxStyle.Question) = MsgBoxResult.Yes Then

            End
        Else

            MsgBox("The System will remain to served your request", MsgBoxStyle.Information, "System Remain")

        End If
    End Sub

    Private Sub BtnShowRecords_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnShowRecords.Click

        Form2.Show() 'this line of code will show the next form and load all records from the database

    End Sub
End Class

This is the next form I create to show all records

'-----------------This is Form2----------------------
'----------------------------------------------------
Public Class Form2
    Dim max As Integer
    Dim inc As Integer

    Dim con As New OleDb.OleDbConnection
    Dim dbprovider As String
    Dim dbsource As String
    Dim ds As New DataSet
    Dim da As OleDb.OleDbDataAdapter
    Dim sql As String
    Dim binding As BindingSource


    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'this line of code automatically load the records once the program run or click show record button

        dbprovider = "PROVIDER = Microsoft.Jet.OLEDB.4.0;"
        dbsource = "Data Source = ../Data/Info.mdb"
        con.ConnectionString = dbprovider & dbsource
        con.Open()
        sql = "SELECT * FROM tblinfo"
        da = New OleDb.OleDbDataAdapter(sql, con)
        da.Fill(ds, "Info")
        Try
            txtID.Text = ds.Tables("Info").Rows(0).Item("ID")
            txtlastname.Text = ds.Tables("Info").Rows(1).Item("Lastname")
            txtfirstname.Text = ds.Tables("Info").Rows(2).Item("Firstname")
            txtmiddlename.Text = ds.Tables("Info").Rows(3).Item("Middlename")
            txtID.Text = ds.Tables("Info").Rows(inc).Item("ID")
            DataGridView1.DataSource = ds.Tables("Info")

            max = ds.Tables("Info").Rows.Count
            Label5.Text = ds.Tables("Info").Rows.Count & " Number of Rows"
            inc = -1
        Catch ex As Exception
            MsgBox("No Rows")
        End Try
    End Sub
    Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
        'this line of code enabled to click datagridview cells row
        If Not IsDBNull(DataGridView1.CurrentRow.Cells(0).Value) Then
            txtID.Text = DataGridView1.CurrentRow.Cells(0).Value
        Else
            txtID.Text = ""
        End If
        If Not IsDBNull(DataGridView1.CurrentRow.Cells(1).Value) Then
            txtlastname.Text = DataGridView1.CurrentRow.Cells(1).Value
        Else
            txtlastname.Text = ""
        End If
        If Not IsDBNull(DataGridView1.CurrentRow.Cells(2).Value) Then
            txtfirstname.Text = DataGridView1.CurrentRow.Cells(2).Value
        Else
            txtfirstname.Text = ""
        End If
        If Not IsDBNull(DataGridView1.CurrentRow.Cells(3).Value) Then
            txtmiddlename.Text = DataGridView1.CurrentRow.Cells(3).Value
        Else
            txtmiddlename.Text = ""
        End If
    End Sub
    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        If MsgBox("Do you want to exit the system?", MsgBoxStyle.YesNo + MsgBoxStyle.Question) = MsgBoxResult.Yes Then
            Me.Close()
        Else
            MsgBox("The System will remain to served your request", MsgBoxStyle.Information, "System Remain")
        End If
    End Sub
End Class

Note: This is an unbound approach of Visual Basic. If you want to make it fast by making a simple or complex program I suggest to used Visual Basic.Net where you can create a program in a minute. There's a lot of function you may used, it depend upon your choice.

0 comments:

Post a Comment