Selanjutnya buat project VB baru, kemudian rancang form barang seperti dibawah ini:
Kemudian, tambahkan module sebagai koneksi antara VB dengan Ms. Access nya:
Imports
System.Data.OleDb
Module
Module1
Public
Conn
As
OleDbConnection
Public
LokasiDB
As
String
Sub
Koneksi()
LokasiDB =
"provider=microsoft.jet.oledb.4.0;data source=dblatihan1.mdb"
Conn =
New
OleDbConnection(LokasiDB)
If
Conn.State = ConnectionState.Closed
Then
Conn.Open()
End
Sub
End
Module
Berikut source code dari aplikasi ini:
Imports
System.Data.OleDb
Public
Class
Form1
Dim
da
As
OleDbDataAdapter
Dim
RD
As
OleDbDataReader
Dim
ds
As
DataSet
Dim
CMD
As
OleDbCommand
Dim
ada1
As
Boolean
Private
Sub
Form1_Load(
ByVal
sender
As
System.
Object
,
ByVal
e
As
System.EventArgs)
Handles
MyBase
.Load
Koneksi()
tampil()
tidakaktif()
btnsimpan.Text =
"Tambah"
tutuptombol
End
Sub
Private
Sub
btnsimpan_Click(
ByVal
sender
As
System.
Object
,
ByVal
e
As
System.EventArgs)
Handles
btnsimpan.Click
If
btnsimpan.Text =
"Tambah"
Then
Call
btnbatal_Click(sender, e)
Exit
Sub
End
If
If
TextBox1.Text =
""
Or
TextBox2.Text =
""
Or
TextBox3.Text =
""
Then
MsgBox(
"Data belum lengkap, Pastikan Kode Admin 6 Digit dan Semua form terisi"
)
Exit
Sub
Else
If
ada1 =
False
Then
Call
Koneksi()
Dim
simpan
As
String
=
"insert into tbBarang values ('"
& TextBox1.Text &
"','"
& TextBox2.Text &
"',"
& TextBox3.Text &
","
& TextBox4.Text &
",'"
& TextBox5.Text &
"')"
CMD =
New
OleDbCommand(simpan, Conn)
CMD.ExecuteNonQuery()
MsgBox(
"Data berhasil di Input"
, MsgBoxStyle.Information,
"Information"
)
Call
tampil()
Call
btnbatal_Click(sender, e)
End
If
If
ada1 =
True
Then
Call
Koneksi()
Dim
edit
As
String
=
"update tbBarang set NamaBarang='"
& TextBox2.Text &
"',hargabarang="
& TextBox3.Text &
",jumlahbarang="
& TextBox4.Text &
", satuan='"
& TextBox5.Text &
"' where KodeBarang='"
& TextBox1.Text &
"'"
CMD =
New
OleDbCommand(edit, Conn)
CMD.ExecuteNonQuery()
MsgBox(
"Data berhasil di Edit"
, MsgBoxStyle.Information,
"Information"
)
tampil()
Call
btnbatal_Click(sender, e)
End
If
End
If
End
Sub
Sub
tampil()
da =
New
OleDbDataAdapter(
"Select * from tbbarang"
, Conn)
ds =
New
DataSet
ds.Clear()
da.Fill(ds,
"tbBarang"
)
DataGridView1.DataSource = (ds.Tables(
"tbBarang"
))
DataGridView1.Columns(0).HeaderText =
"Kode Barang"
DataGridView1.Columns(1).HeaderText =
"Nama Barang"
DataGridView1.Columns(2).HeaderText =
"Harga Barang"
DataGridView1.Columns(3).HeaderText =
"Jumlah Barang"
DataGridView1.Columns(4).HeaderText =
"Satuan"
End
Sub
Private
Sub
TextBox1_KeyPress(
ByVal
sender
As
Object
,
ByVal
e
As
System.Windows.Forms.KeyPressEventArgs)
Handles
TextBox1.KeyPress
If
e.KeyChar = Chr(13)
Then
'Call Koneksi()
CMD =
New
OleDbCommand(
"Select * from tbBarang where kodebarang='"
& TextBox1.Text &
"'"
, Conn)
RD = CMD.ExecuteReader
RD.Read()
If
Not
RD.HasRows
Then
tutuptombol()
ada1 =
False
TextBox2.Text =
""
TextBox3.Text =
""
TextBox4.Text =
""
TextBox5.Text =
""
TextBox2.Focus()
Else
ada1 =
True
bukatombol()
TextBox2.Text = RD.Item(
"NamaBarang"
)
TextBox3.Text = RD.Item(
"hargabarang"
)
TextBox4.Text = RD.Item(
"jumlahbarang"
)
TextBox5.Text = RD.Item(
"satuan"
)
End
If
End
If
End
Sub
Private
Sub
TextBox1_LostFocus(
ByVal
sender
As
Object
,
ByVal
e
As
System.EventArgs)
Handles
TextBox1.LostFocus
Call
Koneksi()
CMD =
New
OleDbCommand(
"Select * from tbBarang where kodebarang='"
& TextBox1.Text &
"'"
, Conn)
RD = CMD.ExecuteReader
RD.Read()
If
Not
RD.HasRows
Then
tutuptombol()
ada1 =
False
bersih()
TextBox2.Focus()
Else
bukatombol()
ada1 =
True
TextBox2.Text = RD.Item(
"NamaBarang"
)
TextBox3.Text = RD.Item(
"hargabarang"
)
TextBox4.Text = RD.Item(
"jumlahbarang"
)
TextBox5.Text = RD.Item(
"satuan"
)
TextBox2.Focus()
End
If
End
Sub
Private
Sub
bersih()
TextBox2.Text =
""
TextBox3.Text =
""
TextBox4.Text =
""
TextBox5.Text =
""
End
Sub
Private
Sub
tidakaktif()
TextBox2.Enabled =
False
TextBox3.Enabled =
False
TextBox4.Enabled =
False
TextBox5.Enabled =
False
End
Sub
Private
Sub
aktif()
TextBox2.Enabled =
True
TextBox3.Enabled =
True
TextBox4.Enabled =
True
TextBox5.Enabled =
True
End
Sub
Private
Sub
bukatombol()
btnedit.Enabled =
True
btnhapus.Enabled =
True
End
Sub
Private
Sub
tutuptombol()
btnedit.Enabled =
False
btnhapus.Enabled =
False
End
Sub
Private
Sub
btnbatal_Click(
ByVal
sender
As
System.
Object
,
ByVal
e
As
System.EventArgs)
Handles
btnbatal.Click
btnsimpan.Text =
"Tambah"
bersih()
tidakaktif()
tutuptombol()
TextBox1.Enabled =
True
TextBox1.Focus()
End
Sub
Private
Sub
btnedit_Click(
ByVal
sender
As
System.
Object
,
ByVal
e
As
System.EventArgs)
Handles
btnedit.Click
aktif()
btnedit.Enabled =
False
TextBox1.Enabled =
False
TextBox2.Focus()
btnsimpan.Text =
"Simpan"
End
Sub
Private
Sub
btnhapus_Click(
ByVal
sender
As
System.
Object
,
ByVal
e
As
System.EventArgs)
Handles
btnhapus.Click
If
MessageBox.Show(
"Yakin akan dihapus..?"
,
""
, MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes
Then
Dim
hapus
As
String
=
"delete * from tbBarang where KodeBarang='"
& TextBox1.Text &
"'"
CMD =
New
OleDbCommand(hapus, Conn)
CMD.ExecuteNonQuery()
MsgBox(
"Data berhasil di Hapus"
, MsgBoxStyle.Information,
"Information"
)
Call
tampil()
Call
btnbatal_Click(sender, e)
Exit
Sub
Else
Call
btnbatal_Click(sender, e)
Exit
Sub
End
If
End
Sub
End
Class
Hasilnya seperti gambar dibawah ini:
Terimakasih :)
Semoga tutorial aplikasi di atas dapat membantu.