VB Net - Cara Edit Database
Berikut Langkahnya :
1. Membuat Database dan Tabel
Buatlah sebuah database dengan nama DBBelajarvb.mdb dan Buatlah Tabel Barang dengan Design Tabel seperti gambar dibawah ini :
2. Membuat Project VB Net dan Design Form
Buka Aplikasi VB Net anda
Simpan di Folder contoh nama Folder : Belajar VB
Masukan Database DBBelajarvb.mdb kedalam Folder Project VB Net Anda yaitu bin\debug
Kemudian buatlah Design Form seperti gambar dibawah ini :
Kemudian masukan Koding dibawah ini :
Imports System.Data.OleDbCara Editnya adalah :
Public Class Form1
Dim CONN As OleDbConnection
Dim CMD As OleDbCommand
Dim DS As New DataSet
Dim DA As OleDbDataAdapter
Dim RD As OleDbDataReader
Dim LokasiDB As String
Sub Koneksi()
LokasiDB = "provider=microsoft.jet.oledb.4.0;data source=DBBelajarvb.mdb"
Conn = New OleDbConnection(LokasiDB)
If Conn.State = ConnectionState.Closed Then Conn.Open()
End Sub
Sub KondisiAwal()
Koneksi()
DA = New OleDbDataAdapter("Select * from barang", CONN)
DS = New DataSet
DS.Clear()
DA.Fill(DS, "Barang")
DataGridView1.DataSource = (DS.Tables("Barang"))
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Call KondisiAwal()
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 = "" Or TextBox3.Text = "" Then
MsgBox("Data belum lengkap, Pastikan Kode Admin 6 Digit dan Semua form terisi")
Exit Sub
Else
Call Koneksi()
Dim edit As String = "update Barang set NamaBarang='" & TextBox2.Text & "',JumlahBarang='" & TextBox3.Text & "' where KodeBarang='" & TextBox1.Text & "'"
CMD = New OleDbCommand(edit, CONN)
CMD.ExecuteNonQuery()
MsgBox("Data berhasil di Edit", MsgBoxStyle.Information, "Information")
Call KondisiAwal()
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
End
End Sub
Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
End Sub
Private Sub DataGridView1_CellContentDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentDoubleClick
Call Koneksi()
End Sub
Private Sub TextBox1_KeyPress1(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 Barang where KodeBarang='" & TextBox1.Text & "'", CONN)
RD = CMD.ExecuteReader
RD.Read()
If Not RD.HasRows Then
TextBox2.Text = ""
TextBox3.Text = ""
TextBox2.Focus()
Else
TextBox2.Text = RD.Item("NamaBarang")
TextBox3.Text = RD.Item("JumlahBarang")
TextBox2.Focus()
End If
End If
End Sub
End Class
Pada text1 atau Kode barang, masukan contoh BRG001, Kemudian Anda ENTER pada Keyboard. Setelah muncul data, ganti data seperti Nama Barang atau Jumlah Barang. Kemudian Klik Simpan.
Semoga bermanfaat untuk anda, Jika ada kesulitan atau ingin bertanya, silahkan berkomentar dibawah
Salam,
BelajarVB.com
0 comments:
Post a Comment