MEMBUAT FORM JENIS BARANG DAN FORM KONSUMEN VB NET 2008
Disini saya akan membahas bagaimana membuat form jenis barang dan form konsumen, langsung saja perhatikan contoh program
dibawah ini.
1.
Pertama pastikan VB net
2008 dan Microsoft Office 2007 agan sudah terinstal di PC agan.
2.
Kedua buat Database di
Microsoft Office Access dengan nama “DbPenjualan”.
3.
Ketiga buat 6 buah tabel :
Gambar 1.1 Tabel Jenis
Barang
Tabel 1.2 Tabel Data
Barang
Gambar 1.3 Tabel
Penjualan
Tabel 1.4 Tabel Detail
Penjualan
Tabel 1.5 Tabel Konsumen
Tabel 1.6 Tabel User
4.
Keempat, Save Database agan.
Buat folder untuk menyimpan program yang akan agan buat dengan nama “Penjualan”
5.
Kelima buka Visual studio
yang sudah agan instal di komputer agan, lalu buat Project dengan nama
“Penjualan”, setelah itu save di folder yang sudah agan buat. Simpan database
yang sudah agan buat di folder Penjualan > Penjualan> bin > Debug.
6.
Keenam buat sebuah module
untuk menghubungkan database yang sudah agan buat dengan program yang akan agan
buat, dengan cara klik kanan pada project Penjualan pilih Add lalu
pilih Module Seperti pada gambar dibawah ini:
7.
Ketujuh, silahkan copykan
Code dibawah ini kedalam Module yang sudah agan buat.
Imports System.Data.OleDb
Module Module1
Public
Conn As OleDbConnection
Public
da As OleDbDataAdapter
Public
ds As DataSet
|
Public
rd As OleDbDataReader
Public Str As
String
Public
Sub Koneksi()
Str = "Provider=Microsoft.ACE.OLEDB.12.0;Data
Source=" & Application.StartupPath & "\DbPenjualan.accdb"
Conn = New OleDbConnection(Str)
If
Conn.State = ConnectionState.Closed Then
Conn.Open()
End
If
End
Sub
End Module
8.
Kedelapan buatlah form
untuk form jenis seperti pada gambar dibawah ini:
Setelah anda membuat form
jenis lalu copykan kode dibawah ini:
Imports System.Data.OleDb
Public Class Form3
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 jenisbarang", Conn)
ds = New DataSet
ds.Clear()
da.Fill(ds, "jenisbarang")
DataGridView1.DataSource =
ds.Tables("jenisbarang")
DataGridView1.Refresh()
End Sub
Sub
AturGrid()
DataGridView1.Columns(0).Width = 85
DataGridView1.Columns(1).Width = 250
DataGridView1.Columns(0).HeaderText = "Kode
Jenis"
DataGridView1.Columns(1).HeaderText = "Nama
Jenis"
End
Sub
Private
Sub Form3_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 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 jenisbarang where kodeJenis='" & TextBox1.Text & "'", Conn)
rd = cmd.ExecuteReader
rd.Read()
If
Not rd.HasRows Then
Dim Simpan As String = "insert
into jenisbarang(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 jenisbarang 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 Jenis belum diisi")
TextBox1.Focus()
Exit
Sub
Else
If
MessageBox.Show("Yakin akan menghapus Data
Jenis " & TextBox2.Text & "
?", "",
MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then
cmd = New OleDbCommand("Delete
* From jenisbarang 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 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 jenisbarang 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
Sub
Private
Sub Button5_Click(ByVal
sender As System.Object, ByVal e As
System.EventArgs) Handles Button5.Click
Me.Close()
End Sub
9.
Kesembilan, selanjutnya
anda membuat form konsumen, seperti pada gambar dibawah ini:
Setelah anda membuat form
konsumen lalu anda copykan kode dibawah ini:
Sub Kosong()
TextBox11.Clear()
TextBox4.Clear()
TextBox5.Clear()
TextBox1.Clear()
TextBox11.Focus()
End
Sub
Sub
Isi()
TextBox4.Clear()
TextBox5.Clear()
TextBox1.Clear()
TextBox11.Focus()
End
Sub
Sub
TampilJenis()
da = New OleDbDataAdapter("Select *
From konsumen", Conn)
ds = New DataSet
ds.Clear()
da.Fill(ds, "konsumen")
DataGridView1.DataSource =
ds.Tables("konsumen")
DataGridView1.Refresh()
End
Sub
Sub
AturGrid()
DataGridView1.Columns(0).Width = 85
DataGridView1.Columns(1).Width = 150
DataGridView1.Columns(2).Width = 250
DataGridView1.Columns(3).Width = 100
DataGridView1.Columns(0).HeaderText = "Kode
Konsumen"
DataGridView1.Columns(1).HeaderText = "Nama
Konsumen"
DataGridView1.Columns(2).HeaderText = "Alamat"
DataGridView1.Columns(3).HeaderText = "No.
Telepon"
End Sub
a. Pada TextBox konsumen copykan kode dibawah ini:
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
If e.KeyChar = Chr(13) Then
cmd = New OleDbCommand("Select
* From konsumen where kd_konsumen='" & TextBox1.Text & "'", Conn)
rd = cmd.ExecuteReader
rd.Read()
If
rd.HasRows = True Then
TextBox2.Text =
rd.Item(1)
TextBox3.Text =
rd.Item(2)
TextBox4.Text =
rd.Item(3)
TextBox11.Focus()
Else
Call Isi()
TextBox2.Focus()
End
If
End If
End Sub
b. Pada TextBox Nama Konsumen copykan kode dibawah ini:
Private Sub TextBox2_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox2.KeyPress
TextBox2.MaxLength = 50
If
e.KeyChar = Chr(13) Then
TextBox2.Text =
UCase(TextBox4.Text)
TextBox3.Focus()
End
If
End Sub
c. Pada TextBox Alamat copykan kode dibawah ini:
Private Sub TextBox3_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox3.KeyPress
TextBox3.MaxLength = 150
If
e.KeyChar = Chr(13) Then
TextBox3.Text =
UCase(TextBox1.Text)
TextBox4.Focus()
End
If
End Sub
d. Pada TextBox Nomor Telepon copykan kode dibawah ini:
Private Sub TextBox4_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox4.KeyPress
TextBox4.MaxLength = 50
If
e.KeyChar = Chr(13) Then
TextBox5.Text =
UCase(TextBox4.Text)
Button1.Focus()
End
If
End Sub
e. Pada Form1_Load ketikan kode dibawah ini:
Private Sub Form1_Load(ByVal sender As
System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Call
Koneksi()
Call
TampilJenis()
Call
Kosong()
Call
AturGrid()
End Sub
f. Pada tombol simpan copykan kode dibawah ini:
Private Sub Button1_Click(ByVal sender As
System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
If
TextBox1.Text = "" Or TextBox2.Text = ""
Or TextBox3.Text = ""
Or TextBox4.Text = ""
Then
MsgBox("Data belum lengkap..!")
TextBox1.Focus()
Exit
Sub
Else
cmd = New OleDbCommand("Select
* From konsumen where kd_konsumen='" & TextBox1.Text & "'", Conn)
rd = cmd.ExecuteReader
rd.Read()
If
Not rd.HasRows Then
Dim Simpan As String = "insert
into konsumen(kd_konsumen, nm_konsumen, alamat, no_telepon)values "
& _
"('" & TextBox1.Text & "','" & TextBox2.Text & "','" & TextBox3.Text & "','" & TextBox4.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
g. Pada tombol ubah copykan kode dibawah ini:
Private Sub Button2_Click(ByVal sender As
System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
If
TextBox1.Text = "" Then
MsgBox("Kode Konsumen belum diisi")
TextBox1.Focus()
Exit
Sub
Else
Dim
ubah As String
= "update konsumen set nm_konsumen='"
& TextBox2.Text & _
"',
alamat='" & TextBox3.Text & _
"',
no_telepon='" & TextBox4.Text & "'
where kd_konsumen='" & 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
h. Pada tombol hapus copykan kode dibawah ini:
Private Sub Button3_Click(ByVal sender As
System.Object, ByVal e As
System.EventArgs) Handles Button3.Click
If
TextBox1.Text = "" Then
MsgBox("Kode Konsumen belum diisi")
TextBox1.Focus()
Exit
Sub
Else
If
MessageBox.Show("Yakin akan menghapus Data
Konsumen" & TextBox2.Text & "
?", "",
MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then
cmd = New OleDbCommand("Delete
* From konsumen where kd_konsumen='" & TextBox1.Text & "'", Conn)
cmd.ExecuteNonQuery()
Call Kosong()
Call TampilJenis()
Else
Call Kosong()
End
If
End
If
End Sub
i. Pada tombol batal copykan kode dibawah ini:
Private Sub Button4_Click(ByVal sender As
System.Object, ByVal e As
System.EventArgs) Handles Button4.Click
Call
Kosong()
End
Sub
j. Pada tombol keluar copykan kode dibawah ini:
Private Sub Button5_Click(ByVal sender As
System.Object, ByVal e As
System.EventArgs) Handles Button5.Click
Me.Close()
End
Sub
0 komentar:
Posting Komentar