Advertisement
rohits134

Phone Book

Jul 10th, 2012
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 4.97 KB | None | 0 0
  1. Public Class Form1
  2.  
  3.     Dim maxRows As Integer
  4.     Dim inc As Integer
  5.     Dim con As OleDb.OleDbConnection = New OleDb.OleDbConnection
  6.     Dim dbProvider As String
  7.     Dim dbSource As String
  8.     Dim ds As New DataSet
  9.     Dim da As OleDb.OleDbDataAdapter
  10.     Dim sql As String
  11.  
  12.     Private Sub NavigateRecords()
  13.  
  14.         txtFirstName.Text = ds.Tables("Address Book").Rows(inc).Item(1)
  15.         txtSurname.Text = ds.Tables("Address Book").Rows(inc).Item(2)
  16.         txtNumber.Text = ds.Tables("Address Book").Rows(inc).Item(3)
  17.        
  18.     End Sub
  19.  
  20.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  21.         dbProvider = "PROVIDER=Microsoft.Jet.OLEDB.4.0;"
  22.         dbSource = "Data Source = Phone Book.mdb"
  23.         con.ConnectionString = dbProvider & dbSource
  24.         con.Open()
  25.         sql = "SELECT * FROM tblNumbers"
  26.         da = New OleDb.OleDbDataAdapter(sql, con)
  27.         da.Fill(ds, "Address Book")
  28.  
  29.         con.Close()
  30.  
  31.         maxRows = ds.Tables("Address Book").Rows.Count
  32.         inc = -1
  33.     End Sub
  34.  
  35.     Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
  36.         If inc <> maxRows - 1 Then
  37.             inc = inc + 1
  38.             NavigateRecords()
  39.         Else
  40.             MsgBox("No More Rows")
  41.         End If
  42.  
  43.     End Sub
  44.  
  45.     Private Sub btnPrevious_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrevious.Click
  46.         If inc > 0 Then
  47.             inc = inc - 1
  48.             NavigateRecords()
  49.         Else
  50.             MsgBox("First Record")
  51.         End If
  52.     End Sub
  53.  
  54.     Private Sub btnLast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLast.Click
  55.         If inc <> maxRows - 1 Then
  56.             inc = maxRows - 1
  57.             NavigateRecords()
  58.         End If
  59.     End Sub
  60.  
  61.     Private Sub btnFirst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFirst.Click
  62.         If inc <> 0 Then
  63.             inc = 0
  64.             NavigateRecords()
  65.         End If
  66.     End Sub
  67.  
  68.     Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
  69.         Dim cb As New OleDb.OleDbCommandBuilder(da)
  70.         ds.Tables("Address Book").Rows(0).Item(0) = "1"
  71.         If inc = -1 Then
  72.             ds.Tables("Address Book").Rows(inc + 1).Item(1) = txtFirstName.Text
  73.             ds.Tables("Address Book").Rows(inc + 1).Item(2) = txtSurname.Text
  74.             ds.Tables("Address Book").Rows(inc + 1).Item(3) = txtNumber.Text
  75.  
  76.         Else
  77.             ds.Tables("Address Book").Rows(inc).Item(1) = txtFirstName.Text
  78.             ds.Tables("Address Book").Rows(inc).Item(2) = txtSurname.Text
  79.             ds.Tables("Address Book").Rows(inc).Item(3) = txtNumber.Text
  80.         End If
  81.  
  82.         da.Update(ds, "Address Book")
  83.  
  84.  
  85.  
  86.         MsgBox("Data updated")
  87.     End Sub
  88.  
  89.     Private Sub btnAddNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddNew.Click
  90.         btnCommit.Enabled = True
  91.         btnAddNew.Enabled = False
  92.         btnUpdate.Enabled = False
  93.         btnDelete.Enabled = False
  94.  
  95.         txtFirstName.Clear()
  96.         txtSurname.Clear()
  97.         txtNumber.Clear()
  98.  
  99.     End Sub
  100.  
  101.     Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
  102.         btnCommit.Enabled = False
  103.         btnAddNew.Enabled = True
  104.         btnUpdate.Enabled = True
  105.         btnDelete.Enabled = True
  106.  
  107.         inc = 0
  108.         NavigateRecords()
  109.     End Sub
  110.  
  111.     Private Sub btnCommit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCommit.Click
  112.         If inc <> -1 Then
  113.  
  114.             Dim cb As New OleDb.OleDbCommandBuilder(da)
  115.             Dim dsNewRow As DataRow
  116.  
  117.             dsNewRow = ds.Tables("Address Book").NewRow()
  118.  
  119.             dsNewRow.Item("FirstName") = txtFirstName.Text
  120.             dsNewRow.Item("Surname") = txtSurname.Text
  121.             dsNewRow.Item("PhoneNumber") = txtNumber.Text
  122.  
  123.             ds.Tables("Address Book").Rows.Add(dsNewRow)
  124.  
  125.             da.Update(ds, "Address Book")
  126.  
  127.             MsgBox("New Record added to the Database")
  128.  
  129.             btnCommit.Enabled = False
  130.             btnAddNew.Enabled = True
  131.             btnUpdate.Enabled = True
  132.             btnDelete.Enabled = True
  133.  
  134.         End If
  135.     End Sub
  136.  
  137.     Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
  138.         If MessageBox.Show("Do you really want to Delete this Record?", "Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) = DialogResult.No Then
  139.             MsgBox("Operation Cancelled")
  140.             Exit Sub
  141.  
  142.         End If
  143.         Dim cb As New OleDb.OleDbCommandBuilder(da)
  144.  
  145.         ds.Tables("Address Book").Rows(inc).Delete()
  146.         maxRows = maxRows - 1
  147.  
  148.         inc = 0
  149.         NavigateRecords()
  150.         da.Update(ds, "Address Book")
  151.     End Sub
  152.  
  153. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement