Database Connection: Objective
Database Connection: Objective
Objective
In this chapter, you will learn about : To make connection to Microsoft Access database using Visual Basic 2008. To Read (Select) and write (Insert, Update & Delete) data from/to table in the database. Students Note
7.0 Introduction
Before we start doing this session, download/copy AddressBook.mdb database and save it into D: directory. The AddressBook.mdb has only one table: tblContacts, which the detail as follows: Field Name ID FirstName Surname Data Type Number Text Text Field Size Long Integer 20 20
We will access the database and read or write the data from/to its table. The type of access can be: 1. Read data . 2. Insert/add new data 3. Search data 4. Update data 5. Delete data
Students Note
7.1 (a)
7.1(b)
Properties
Text Name Text Text Text Name Name Name Text Name Text Name Text Name Text Name Text Name Text Name Enabled Text Name Enabled Text Name Text Name Text Name Text Name AddressBook frmAddressBook ID FirstName Surname txtID txtFirstName txtSurname << btnFirst < btnPrevious > btnNext >> btnLast Add/Insert btnAdd Commit btnCommit False Cancel btnCancel False Search btnSearch Update btnUpdate Delete btnDelete Close btnClose
Value
Students Note
Button2
Button3
Button4
Button5
Button6
Button7
Button8
Button9
Button10
Button9
Students Note
Students Note
7.4 Code to display the first, previous, next or Last data in the table
'Display First Record if the table is not empty Private Sub btnFirst_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnFirst.Click If MaxRows > 0 Then If baris = 0 Then MsgBox("First Record") End If baris = 0 DisplayRecord() Else MsgBox("Empty Table") End If End Sub 'Display Previous Record if the table is not empty Private Sub btnPrevious_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnPrevious.Click If MaxRows > 0 Then If baris > 0 Then baris = baris - 1 Else MsgBox("First Record") End If DisplayRecord() Else MsgBox("Empty Table") End If End Sub 'Display Next Record if the table is not empty Private Sub btnNext_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnNext.Click If MaxRows > 0 Then If baris < MaxRows - 1 Then baris = baris + 1 Else MsgBox("Last Record!") End If DisplayRecord() Else MsgBox("Empty Table") End If End Sub
Students Note
'Display Last Record if the table is not empty Private Sub btnLast_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnLast.Click If MaxRows > 0 Then If baris = MaxRows - 1 Then MsgBox("Last Record!") End If baris = MaxRows - 1 DisplayRecord() Else MsgBox("Empty Table") End If End Sub
Students Note
The code are related to four buttons: <<, <, > and >> button.
Students Note
btnAdd.Enabled = True btnCommit.Enabled = False btnCancel.Enabled = False btnSearch.Enabled = True btnUpdate.Enabled = True btnDelete.Enabled = True btnClose.Enabled = True btnFirst.Enabled = True btnPrevious.Enabled = True btnNext.Enabled = True btnLast.Enabled = True txtID.Enabled = False 'display the last record baris = MaxRows - 1 DisplayRecord() Else MsgBox("The ID is duplicated") End If Else MsgBox("The ID should be Integer Number and No Empty TextBox") End If End If End Sub
Students Note
Students Note
Students Note
Students Note
Students Note
Students Note