Oleh: admin | 30/03/2015

Source Simpan, Edit, Hapus, Batal pada vb2010

Sebelumnya buat database terlebih dahulu dengan nama dblatihan1 pada Ms.Access 2007. Simpan dengan format 2002-2003, kemudian lanjutkan buat tabel dengan nama tbbarang dengan struktur tabel seperti gambar dibawah ini:

tbbarang

Selanjutnya buat project baru, kemudian rancang form barang seperti dibawah ini:

form barang vbnet 2010

 

Tambahkan model untuk koneksi, masukkan source dibawah ini:

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

Selanjutnya, ketik source penyimpan, edit, hapus dan batal dibawah 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 berikut:

form barang vb 2010

Semoga bermanfaat,,, dapatkan source code lebih banyak untuk vbNet (2008,2010) DISINI.


Responses

  1. maaf semua dah di copas pass di coba berjalan normal masih susah untuk memasukan data barang baru m,, mohon balasnnya.maaf tahap awal nieh

    • bisa ko, susahnya gmana?


Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s

Kategori

%d blogger menyukai ini: