Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Public Class Form1
- Dim maxRows As Integer
- Dim inc As Integer
- Dim con As OleDb.OleDbConnection = 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
- Private Sub NavigateRecords()
- txtFirstName.Text = ds.Tables("Address Book").Rows(inc).Item(1)
- txtSurname.Text = ds.Tables("Address Book").Rows(inc).Item(2)
- txtNumber.Text = ds.Tables("Address Book").Rows(inc).Item(3)
- End Sub
- Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
- dbProvider = "PROVIDER=Microsoft.Jet.OLEDB.4.0;"
- dbSource = "Data Source = Phone Book.mdb"
- con.ConnectionString = dbProvider & dbSource
- con.Open()
- sql = "SELECT * FROM tblNumbers"
- da = New OleDb.OleDbDataAdapter(sql, con)
- da.Fill(ds, "Address Book")
- con.Close()
- maxRows = ds.Tables("Address Book").Rows.Count
- inc = -1
- End Sub
- Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
- If inc <> maxRows - 1 Then
- inc = inc + 1
- NavigateRecords()
- Else
- MsgBox("No More Rows")
- 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()
- Else
- MsgBox("First Record")
- End If
- End Sub
- Private Sub btnLast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLast.Click
- If inc <> maxRows - 1 Then
- inc = maxRows - 1
- NavigateRecords()
- 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()
- End If
- End Sub
- Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
- Dim cb As New OleDb.OleDbCommandBuilder(da)
- ds.Tables("Address Book").Rows(0).Item(0) = "1"
- If inc = -1 Then
- ds.Tables("Address Book").Rows(inc + 1).Item(1) = txtFirstName.Text
- ds.Tables("Address Book").Rows(inc + 1).Item(2) = txtSurname.Text
- ds.Tables("Address Book").Rows(inc + 1).Item(3) = txtNumber.Text
- Else
- ds.Tables("Address Book").Rows(inc).Item(1) = txtFirstName.Text
- ds.Tables("Address Book").Rows(inc).Item(2) = txtSurname.Text
- ds.Tables("Address Book").Rows(inc).Item(3) = txtNumber.Text
- End If
- da.Update(ds, "Address Book")
- MsgBox("Data updated")
- End Sub
- Private Sub btnAddNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddNew.Click
- btnCommit.Enabled = True
- btnAddNew.Enabled = False
- btnUpdate.Enabled = False
- btnDelete.Enabled = False
- txtFirstName.Clear()
- txtSurname.Clear()
- txtNumber.Clear()
- End Sub
- Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
- btnCommit.Enabled = False
- btnAddNew.Enabled = True
- btnUpdate.Enabled = True
- btnDelete.Enabled = True
- inc = 0
- NavigateRecords()
- End Sub
- Private Sub btnCommit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCommit.Click
- If inc <> -1 Then
- Dim cb As New OleDb.OleDbCommandBuilder(da)
- Dim dsNewRow As DataRow
- dsNewRow = ds.Tables("Address Book").NewRow()
- dsNewRow.Item("FirstName") = txtFirstName.Text
- dsNewRow.Item("Surname") = txtSurname.Text
- dsNewRow.Item("PhoneNumber") = txtNumber.Text
- ds.Tables("Address Book").Rows.Add(dsNewRow)
- da.Update(ds, "Address Book")
- MsgBox("New Record added to the Database")
- btnCommit.Enabled = False
- btnAddNew.Enabled = True
- btnUpdate.Enabled = True
- btnDelete.Enabled = True
- End If
- End Sub
- Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.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 cb As New OleDb.OleDbCommandBuilder(da)
- ds.Tables("Address Book").Rows(inc).Delete()
- maxRows = maxRows - 1
- inc = 0
- NavigateRecords()
- da.Update(ds, "Address Book")
- End Sub
- End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement