Anda di halaman 1dari 5

Aplikasi Penjualan Buku Menggunakan Vb.

Net dan Access

Step 1 : Membuat project baru

Langkah – langkah dalam membuat aplikasi sebagai berikut :

A. Buat project baru pada VB. Net seperti pada gambar dibawah ini.
Pada tutorial ini menggunakan VB 2010 dan Access

Klik New Project. Akan tampil menu seperti gambar dibawah ini.

1. Pastikan pilih WindowsFormAplication


2. Ubah WindosApplication1 menjadi Aplikasi Penjualan
3. Klik OK
Lokasi untuk simpan aplikasi bisa disesuaikan dengan keinginan. (Klik Browse untuk
menyesuaikan penyimpanan aplikasi yang baru dibuat)

Klik kanan Form1.vb pada Solution Explore lalu Rename menjadi FormJenis.vb dan
rancang lah tampilan form seperti berikut :

Property Setting :

Object Name Text


Form 1 FormKode Data Kode Buku
Label 1 Label 1.Text Kode
Label 2 Label 2.Text Jenis Buku
TextBox 1 TextBox 1.Text -
TextBox 2 TextBox 1.Text -
Button 1 Button 1 &Simpan
Button 2 Button 2 &Ubah
Button 3 Button 3 &Hapus
Button 4 Button 4 &Batal
DataGridView1 DataGridView1 -

Double klik pada setiap object ketikan kode berikut :

Imports System.Data.OleDb
Public Class FormKode
Sub Kosong()
TextBox1.Clear()
TextBox2.Clear()
TextBox1.Focus()
End Sub
Sub Isi()
TextBox2.Clear()
TextBox2.Focus()
End Sub
Sub TampilJenis()
da = New OleDbDataAdapter("Select * From Jenis", Conn)
ds = New DataSet
ds.Clear()
da.Fill(ds, "Jenis")
DataGridView1.DataSource = ds.Tables("Jenis")
DataGridView1.Refresh()
End Sub
Sub AturGrid()
DataGridView1.Columns(0).Width = 100
DataGridView1.Columns(1).Width = 290
DataGridView1.Columns(0).HeaderText = "KODE JENIS"
DataGridView1.Columns(1).HeaderText = "NAMA JENIS"
End Sub

Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)


Handles MyBase.Load
Call Koneksi()
Call TampilJenis()
Call Kosong()
Call AturGrid()
End Sub

Private Sub TextBox2_KeyPress(ByVal sender As System.Object, ByVal e As


System.Windows.Forms.KeyPressEventArgs) Handles TextBox2.KeyPress
TextBox2.MaxLength = 50
If e.KeyChar = Chr(13) Then
TextBox2.Text = UCase(TextBox2.Text)
End If
End Sub

Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e


As System.Windows.Forms.DataGridViewCellEventArgs) Handles
DataGridView1.CellContentClick
Dim i As Integer
i = Me.DataGridView1.CurrentRow.Index
With DataGridView1.Rows.Item(i)
Me.TextBox1.Text = .Cells(0).Value
Me.TextBox2.Text = .Cells(1).Value
End With
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button1.Click
If TextBox1.Text = "" Or TextBox2.Text = "" Then
MsgBox("Data belum lengkap..!")
TextBox1.Focus()
Exit Sub
Else
cmd = New OleDbCommand("Select * From Jenis where KodeJenis='" &
TextBox1.Text & "'", Conn)
rd = cmd.ExecuteReader
rd.Read()
If Not rd.HasRows Then
Dim Simpan As String = "insert into Jenis(KodeJenis,Jenis)values " & _
"('" & TextBox1.Text & "','" & TextBox2.Text & "')"
cmd = New OleDbCommand(Simpan, Conn)
cmd.ExecuteNonQuery()
MsgBox("Simpan data sukses...!", MsgBoxStyle.Information, "Perhatian")
End If
Call TampilJenis()
Call Kosong()
TextBox1.Focus()
End If
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button2.Click
If TextBox1.Text = "" Then
MsgBox("Kode Jenis belum diisi")
TextBox1.Focus()
Exit Sub
Else
Dim Ubah As String = "Update Jenis set " & _
"Jenis='" & TextBox2.Text & "' " & _
"where KodeJenis='" & TextBox1.Text & "'"
cmd = New OleDbCommand(Ubah, Conn)
cmd.ExecuteNonQuery()
MsgBox("Ubah data sukses..!", MsgBoxStyle.Information, "Perhatian")
Call TampilJenis()
Call Kosong()
TextBox1.Focus()
End If
End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button3.Click
If TextBox1.Text = "" Then
MsgBox("Kode Buku belum diisi")
TextBox1.Focus()
Exit Sub
Else
If MessageBox.Show("Yakin akan menghapus Data Jenis " & TextBox1.Text & "
?", "", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then
cmd = New OleDbCommand("Delete * From Jenis where KodeJenis='" &
TextBox1.Text & "'", Conn)
cmd.ExecuteNonQuery()
Call Kosong()
Call TampilJenis()
Else
Call Kosong()
End If
End If
End Sub

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button4.Click
Call Kosong()
End Sub

Private Sub TextBox1_KeyPress(ByVal sender As System.Object, ByVal e As


System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
TextBox1.MaxLength = 2
If e.KeyChar = Chr(13) Then
cmd = New OleDbCommand("Select * From Jenis where KodeJenis='" &
TextBox1.Text & "'", Conn)
rd = cmd.ExecuteReader
rd.Read()
If rd.HasRows = True Then
TextBox2.Text = rd.Item(1)
TextBox2.Focus()
Else
Call Isi()
TextBox2.Focus()
End If
End If
End Sub
End Class

B. Membuat Module
Module ini adalah sebagai koneksi antara aplikasi yang akan dibuat dengan database
Access 2007.
Klik kanan aplikasi penjualan pilih add > add module dan Ketikan kode berikut pada
Module1 :
Imports System.Data.OleDb
Module Module1
Public Conn As OleDbConnection
Public da As OleDbDataAdapter
Public ds As DataSet
Public cmd As OleDbCommand
Public rd As OleDbDataReader
Public Str As String
Public Sub Koneksi()
Str = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" &
Application.StartupPath & "\Buku.accdb"
Conn = New OleDbConnection(Str)
If Conn.State = ConnectionState.Closed Then
Conn.Open()
End If
End Sub
End Module

C. Membuat Database Access


Buat lah database baru dengan nama Buku.accdb, lalu tambahkan tabel Jenis. Simpan
database pada folder Aplikasi Penjualan > Bin > Debug.

Struktur Tabel Jenis :

Field Type Size Keterangan


KodeJenis Text 5 PK (Primary Key)
Jenis Text 50

Anda mungkin juga menyukai