Nama Database : Karaoke
Nama Tabel : Tamu
Tabel Tamu dengan
struktur sebagai berikut:
Nama Field
|
Type
|
Jumlah
|
Kode
|
Text
|
10
|
Nama
|
Text
|
30
|
NoHp
|
Text
|
20
|
Jumlah Orang
|
Text
|
10
|
Design Form Data Tamu
Listing Program
Imports
System.Data.OleDb
Public Class Form1
Public db As OleDbConnection
Public cmd As OleDbCommand
Public adp As OleDbDataAdapter
Public ds As New DataSet
Dim sql As String
Public dr As OleDbDataReader
Public Sub opendb()
Dim
lokasi = "provider=microsoft.jet.oledb.4.0;data
source=c:\vb2\karaoke.mdb"
db = New
OleDbConnection(lokasi)
If
db.State = ConnectionState.Closed Then
db.Open()
End If
End Sub
Sub
tampiltamu()
adp = New
OleDbDataAdapter("SELECT * FROM tamu",
db)
ds = New
DataSet
adp.Fill(ds, "tamu")
DataGridView1.DataSource = ds.Tables("tamu")
End Sub
Sub
buttonbaru()
Btnbaru.Enabled = True
Btnsimpan.Enabled = False
Btnedit.Enabled = False
Btnhapus.Enabled = False
Btnbatal.Enabled = False
Btnkeluar.Enabled = True
End Sub
Sub
buttonedit()
Btnbaru.Enabled = False
Btnsimpan.Enabled = False
Btnedit.Enabled = True
Btnhapus.Enabled = True
Btnbatal.Enabled = True
Btnkeluar.Enabled = False
End Sub
Sub
buttonsimpan()
Btnbaru.Enabled = False
Btnsimpan.Enabled = True
Btnedit.Enabled = False
Btnhapus.Enabled = False
Btnbatal.Enabled = True
Btnkeluar.Enabled = False
End Sub
Private Sub Form1_Load(ByVal
sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Call
opendb()
Call
hapustext()
Call
tampiltamu()
End Sub
Sub
hapustext()
kode.Text = ""
Nama.Text = ""
NoHp.Text = ""
JumlahOrang.Text = ""
Call
buttonbaru()
End Sub
Private Sub Btnbaru_Click(ByVal
sender As System.Object, ByVal e As
System.EventArgs) Handles Btnbaru.Click
Call
hapustext()
Kode.Focus()
End Sub
Private Sub Kode_KeyPress(ByVal
sender As Object,
ByVal e As
System.Windows.Forms.KeyPressEventArgs) Handles
Kode.KeyPress
Dim
tombol As Integer
= Asc(e.KeyChar)
Dim
xkode As String
If
tombol = 13 Then
sql = "SELECT
* FROM tamu where kode='" & Kode.Text & "'"
cmd = New
OleDbCommand(sql, db)
dr = cmd.ExecuteReader
dr.Read()
If
dr.HasRows = True Then
Nama.Text = dr.Item(1)
NoHp.Text = dr.Item(2)
JumlahOrang.Text = dr.Item(3)
Call
buttonedit()
MsgBox("Data
ditemukan !")
Else
xkode = Kode.Text
Call
hapustext()
Kode.Text = xkode
Call
buttonsimpan()
MsgBox("Data
Tidak ditemukan !")
End
If
Nama.Focus()
NoHp.Focus()
JumlahOrang.Focus()
End If
End Sub
Private Sub Btnedit_Click(ByVal
sender As System.Object, ByVal e As
System.EventArgs) Handles Btnedit.Click
Call
prosesdb(1)
End Sub
Private Sub Btnhapus_Click(ByVal
sender As System.Object, ByVal e As
System.EventArgs) Handles Btnhapus.Click
Call
prosesdb(2)
End Sub
Private Sub Btnkeluar_Click(ByVal
sender As System.Object, ByVal e As
System.EventArgs) Handles Btnkeluar.Click
Me.Close()
End Sub
Private Sub Btnbatal_Click(ByVal
sender As System.Object, ByVal e As
System.EventArgs) Handles Btnbatal.Click
Call
hapustext()
Kode.Focus()
End Sub
Private Sub Btnsimpan_Click(ByVal
sender As System.Object, ByVal e As
System.EventArgs) Handles Btnsimpan.Click
Call
prosesdb(0)
End Sub
Sub
prosesdb(ByVal aktifitas As Byte)
Select Case aktifitas
Case
0
sql = "INSERT
INTO tamu (Kode, Nama, NoHp, JumlahOrang)" & _
" VALUES('" & Kode.Text & "','" & _
Nama.Text & "','" &
_
NoHp.Text & "','" &
_
JumlahOrang.Text & "')"
Case
1
sql = "UPDATE
tamu SET Nama='" & Nama.Text & "',"
& _
" NoHp='" & NoHp.Text & "'," & _
"JumlahOrang='" & JumlahOrang.Text
& "' " & _
" where Kode='" & Kode.Text & "'"
Case
2
sql = "DELETE
FROM tamu where Kode='" & Kode.Text & "'"
End Select
cmd = New
OleDbCommand(sql, db)
cmd.ExecuteNonQuery()
Call
hapustext()
Call
tampiltamu()
End Sub
End Class
Form Saat Program di Jalankan
Apabila kita ingin menyimpan data diatas kita hanya perlu
klik menu Simpan pada form diatas, maka akan menampilkan hasil
seperti berikut ini:
Apabila kita ingin mengedit
maka
akan tampil tampilan seperti di bawah ini:
Lalu ubah data yang ingin anda ubah, misalnya jumlah orang 2 kemudian klik menu Edit. Maka akan menampilkan hasil
seperti berikut ini:
Dan apabila kita ingin menghapus maka akan tampil tampilan seperti di bawah ini:
Lalu anda klik menu Hapus
maka file yang anda pilih akan terhapus, maka akan tampil tampilan seperti ini: