(Bahan
Blog)
Cara
Membuat Windows Form Simpan,Edit dan Hapus di VB 2010
Hallo saya Novri Rizqullah sebagai pemilik blog ini akan
memberikan sedikit hal tentang Software yang satu ini yaitu VB (Visual Basic)
disini saya masih memakai VB 2010 dan setahu saya VB yang baru dirilis adalah
VB 2012.
DiVB
2010 saya akan membagikan/menshare cara membuat form yang bisa
menyimpan,mengedit,dan menghapus suatu data,hal-hal yang harus disiapkan adalah
1. Pastikan Anda sudah mempunyai software VB(Visual Basic) yang saya sarankan
adalah 2010 dan 2012 karena skrip yang digunakan sama dan tidak terlalu
berbeda.
2.
Siapkan Software Microsoft Office yaitu “Microsoft Accses” sebagai database nya
anda bisa menggunakan 2007,2010 maupun 2013.
Maka inilah cara pembuatannya:
1.
1. Buatlah Databasenya terlebih dahulu di Microsoft
Accses (disini saya membuat database tentang biodata). Buatlah seperti gambar
dibawah ini
Dari
gambar diatas saya memakai nama saya sebagai databasenya (novri_rizqullah) dan
tabelnya (biodata) anda bisa mengganti nama databasenya dan tabelnya dengan
yang anda inginkan.
Imports System.Data.OleDb
Public Class Form10
Dim CONN As OleDbConnection
Dim cmd As OleDbCommand
Dim cmd1 As OleDbCommand
Dim ds As New DataSet
Dim dt As New DataTable
Dim da As OleDbDataAdapter
Dim rd As OleDbDataReader
Dim lokasidb As String
Sub koneksi()
lokasidb = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=novri_rizqullah.accdb"
CONN = New OleDbConnection(lokasidb)
If CONN.State = ConnectionState.Closed Then CONN.Open()
End Sub
Sub KondisiAwal()
koneksi()
da = New OleDbDataAdapter("select * From biodata", CONN)
ds = New DataSet
ds.Clear()
da.Fill(ds, "biodata")
DataGridView1.DataSource = (ds.Tables("biodata"))
End Sub
Sub Kosongkan()
TextBox1.Clear()
TextBox2.Clear()
TextBox3.Clear()
TextBox5.Clear()
TextBox6.Clear()
TextBox7.Clear()
TextBox8.Clear()
TextBox9.Clear()
TextBox1.Focus()
End Sub
Sub TampilGrid()
da = New OleDbDataAdapter("select*from biodata", CONN)
ds = New DataSet
da.Fill(ds, "biodata")
DataGridView1.DataSource = ds.Tables("biodata")
DataGridView1.ReadOnly = True
End Sub
Sub carikode()
cmd = New OleDbCommand("select * from biodata where Nis='" & TextBox1.Text & "'", CONN)
rd = cmd.ExecuteReader
rd.Read()
End Sub
Private Sub Form10_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Call KondisiAwal()
Call koneksi()
Call TampilGrid()
Call Kosongkan()
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 = "" Or TextBox5.Text = "" Or TextBox6.Text = "" Or TextBox7.Text = "" Or TextBox8.Text = "" Or TextBox9.Text = "" Then
MsgBox("data belum lengkap,isi lagi")
TextBox1.Focus()
TextBox1.Text = ""
Exit Sub
Else
Call koneksi()
Dim simpan As String = "insert into biodata values ('" & TextBox1.Text & "','" & TextBox2.Text & "','" & TextBox3.Text & "','" & DateTimePicker1.Value & "','" & TextBox5.Text & "','" & TextBox6.Text & "','" & TextBox7.Text & "','" & TextBox8.Text & "','" & TextBox9.Text & "')"
cmd = New OleDbCommand(simpan, CONN)
cmd.ExecuteNonQuery()
MsgBox("data berhasil disimpan", MsgBoxStyle.Information, "information")
Call KondisiAwal()
TextBox1.Text = ""
TextBox2.Text = ""
TextBox3.Text = ""
TextBox5.Text = ""
TextBox6.Text = ""
TextBox7.Text = ""
TextBox8.Text = ""
TextBox9.Text = ""
TextBox1.Focus()
TextBox2.Focus()
TextBox3.Focus()
DateTimePicker1.Focus()
TextBox5.Focus()
TextBox6.Focus()
TextBox7.Focus()
TextBox8.Focus()
TextBox9.Focus()
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Call koneksi()
cmd = New OleDbCommand("Select* from biodata where Nis='" & TextBox1.Text & "'", CONN)
rd = cmd.ExecuteReader
rd.Read()
Dim edit As String = "update biodata set Nama='" & TextBox2.Text & "',TempatLahir='" & TextBox3.Text & "',TanggalLahir='" & DateTimePicker1.Value & "',Alamat='" & TextBox5.Text & "',Ibu='" & TextBox6.Text & "',Ayah='" & TextBox7.Text & "',Email='" & TextBox8.Text & "',NOHP='" & TextBox9.Text & "' where Nis='" & TextBox1.Text & "'"
cmd = New OleDbCommand(edit, CONN)
cmd.ExecuteNonQuery()
MsgBox("Data Berhasil diubah", MsgBoxStyle.Information)
Call Kosongkan()
Call TampilGrid()
End Sub
Private Sub TextBox1_KeyPress1(ByVal paintsender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
If e.KeyChar = Chr(13) Then
Call koneksi()
cmd = New OleDbCommand("select * from biodata where Nis='" & TextBox1.Text & "'", CONN)
rd = cmd.ExecuteReader
rd.Read()
If Not rd.HasRows Then
TextBox2.Text = ""
TextBox3.Text = ""
TextBox5.Text = ""
TextBox6.Text = ""
TextBox7.Text = ""
TextBox8.Text = ""
TextBox2.Focus()
Else
TextBox2.Text = rd.Item("Nama")
TextBox3.Text = rd.Item("TempatLahir")
DateTimePicker1.Value = rd.Item("TanggalLahir")
TextBox5.Text = rd.Item("Alamat")
TextBox6.Text = rd.Item("Ibu")
TextBox7.Text = rd.Item("Ayah")
TextBox8.Text = rd.Item("Email")
TextBox9.Text = rd.Item("NOHP")
TextBox2.Focus()
End If
End If
End Sub
Private Sub TextBox2_KeyPress1(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox2.KeyPress
If e.KeyChar = Chr(13) Then TextBox3.Focus()
End Sub
Private Sub TextBox3_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox3.KeyPress
If e.KeyChar = Chr(13) Then TextBox5.Focus()
End Sub
Private Sub TextBox5_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox5.KeyPress
If e.KeyChar = Chr(13) Then TextBox6.Focus()
End Sub
Private Sub TextBox6_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox6.KeyPress
If e.KeyChar = Chr(13) Then TextBox7.Focus()
End Sub
Private Sub TextBox7_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox7.KeyPress
If e.KeyChar = Chr(13) Then TextBox8.Focus()
End Sub
Private Sub TextBox8_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox8.KeyPress
If e.KeyChar = Chr(13) Then TextBox9.Focus()
End Sub
Private Sub TextBox9_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox9.KeyPress
If e.KeyChar = Chr(13) Then Button2.Focus()
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim response As MsgBoxResult
response = MsgBox(" Anda Yakin Mau keluar dari Sini", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "Pembuat Novri Rizqullah")
If response = MsgBoxResult.Yes Then
MsgBox("Terima Kasih", MsgBoxStyle.Information, "Pembuat Novri Rizqullah")
Me.Dispose()
ElseIf response = MsgBoxResult.No Then
MsgBox("Selamat Melanjutkan", MsgBoxStyle.Information, "Pembuat Novri Rizqullah")
End If
Exit Sub
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
TextBox1.Text = ""
TextBox2.Text = ""
TextBox3.Text = ""
TextBox5.Text = ""
TextBox6.Text = ""
TextBox7.Text = ""
TextBox8.Text = ""
TextBox9.Text = ""
TextBox1.Focus()
End Sub
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
If TextBox1.Text = "" Then
MsgBox("Nis masih kosong,silahkan diisi dulu")
TextBox1.Focus()
Exit Sub
Else
If MessageBox.Show("Yakin akan dihapus..?", "", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then
Dim hapus As String = "delete * from biodata where Nis='" & TextBox1.Text & "'"
cmd = New OleDbCommand(hapus, CONN)
cmd.ExecuteNonQuery()
MsgBox("data berhasil dihapus", MsgBoxStyle.Information, "information")
Call TampilGrid()
Call Kosongkan()
Else
Call Kosongkan()
End If
End If
End Sub
Private Sub DataGridView1_CellMouseClick(ByVal sender As Object, ByVal e As DataGridViewCellMouseEventArgs) Handles DataGridView1.CellMouseClick
On Error Resume Next
TextBox1.Text = DataGridView1.Rows(e.RowIndex).Cells(0).Value
TextBox2.Text = DataGridView1.Rows(e.RowIndex).Cells(1).Value
TextBox3.Text = DataGridView1.Rows(e.RowIndex).Cells(2).Value
DateTimePicker1.Value = DataGridView1.Rows(e.RowIndex).Cells(3).Value
TextBox5.Text = DataGridView1.Rows(e.RowIndex).Cells(4).Value
TextBox6.Text = DataGridView1.Rows(e.RowIndex).Cells(5).Value
TextBox7.Text = DataGridView1.Rows(e.RowIndex).Cells(6).Value
TextBox8.Text = DataGridView1.Rows(e.RowIndex).Cells(7).Value
TextBox9.Text = DataGridView1.Rows(e.RowIndex).Cells(8).Value
Call carikode()
If rd.HasRows Then
Call KondisiAwal()
Call koneksi()
Call TampilGrid()
End If
End Sub
End Class
1
2.Jika sudah selesai membuat Databasenya ,buka
software Vb 2010/2012 dan buatlah tampilan Formnya, berikut ini contohnya:
Dari gambar diatas saya sudah menandai
“Textbox dan Button” karena didua inilah kita akan memasukkan skripnya jadi
tidak boleh salah sedikit pun baik TextBox maupun Button karena bisa berakibat
fatal pada saat Program dijalankan.
NB(Jangan lupa disana saya menaruh
“DataGridView1” sebagai tempat output nanti yang akan tersambung dengan
database anda).
1
3.Langkah selanjutnya adalah memasukkan skripnya,ini adalah kilasan gambar dari skrip tersebut:
3.Langkah selanjutnya adalah memasukkan skripnya,ini adalah kilasan gambar dari skrip tersebut:
sesuai gambar diatas saya sudah menandai
bagian-bagian mana saja yang akan diubah.
Gambar Diatas adalah Skrip Simpan dan yang
lainnya….(maaf saya tidak bisa menyebutkan satu persatu)
Diatas adalah skrip Edit atau Ubah….
Berikut adalah skrip dimana jika kita
memasukkan Nisn saja maka semua data akan muncul karena Kolom Nisn merupakan
“Primary Key”.
Diatas merupakan skrip tombol keluar dari Windows Form dijalankan….
Gambar ini merupakan skrip untuk menghapus
data dari database dan juga untuk tombol batal ……..
Untuk Skrip diatas sebenarnya tidak terlalu
penting karena gunanya saja untuk menampilan data di “DataGridView” kembali ke
Windows Form,dengan Klik Pada DataGridView saja.
Dan Seperti Inilah Hasilnya :
Masih kurang paham? atau malas mengetik
skripnya?,tenang saya sudah menyiapkan Skrip yang tinggal anda Copy Paste saja
dibawah ini…..
Imports System.Data.OleDb
Public Class Form10
Dim CONN As OleDbConnection
Dim cmd As OleDbCommand
Dim cmd1 As OleDbCommand
Dim ds As New DataSet
Dim dt As New DataTable
Dim da As OleDbDataAdapter
Dim rd As OleDbDataReader
Dim lokasidb As String
Sub koneksi()
lokasidb = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=novri_rizqullah.accdb"
CONN = New OleDbConnection(lokasidb)
If CONN.State = ConnectionState.Closed Then CONN.Open()
End Sub
Sub KondisiAwal()
koneksi()
da = New OleDbDataAdapter("select * From biodata", CONN)
ds = New DataSet
ds.Clear()
da.Fill(ds, "biodata")
DataGridView1.DataSource = (ds.Tables("biodata"))
End Sub
Sub Kosongkan()
TextBox1.Clear()
TextBox2.Clear()
TextBox3.Clear()
TextBox5.Clear()
TextBox6.Clear()
TextBox7.Clear()
TextBox8.Clear()
TextBox9.Clear()
TextBox1.Focus()
End Sub
Sub TampilGrid()
da = New OleDbDataAdapter("select*from biodata", CONN)
ds = New DataSet
da.Fill(ds, "biodata")
DataGridView1.DataSource = ds.Tables("biodata")
DataGridView1.ReadOnly = True
End Sub
Sub carikode()
cmd = New OleDbCommand("select * from biodata where Nis='" & TextBox1.Text & "'", CONN)
rd = cmd.ExecuteReader
rd.Read()
End Sub
Private Sub Form10_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Call KondisiAwal()
Call koneksi()
Call TampilGrid()
Call Kosongkan()
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 = "" Or TextBox5.Text = "" Or TextBox6.Text = "" Or TextBox7.Text = "" Or TextBox8.Text = "" Or TextBox9.Text = "" Then
MsgBox("data belum lengkap,isi lagi")
TextBox1.Focus()
TextBox1.Text = ""
Exit Sub
Else
Call koneksi()
Dim simpan As String = "insert into biodata values ('" & TextBox1.Text & "','" & TextBox2.Text & "','" & TextBox3.Text & "','" & DateTimePicker1.Value & "','" & TextBox5.Text & "','" & TextBox6.Text & "','" & TextBox7.Text & "','" & TextBox8.Text & "','" & TextBox9.Text & "')"
cmd = New OleDbCommand(simpan, CONN)
cmd.ExecuteNonQuery()
MsgBox("data berhasil disimpan", MsgBoxStyle.Information, "information")
Call KondisiAwal()
TextBox1.Text = ""
TextBox2.Text = ""
TextBox3.Text = ""
TextBox5.Text = ""
TextBox6.Text = ""
TextBox7.Text = ""
TextBox8.Text = ""
TextBox9.Text = ""
TextBox1.Focus()
TextBox2.Focus()
TextBox3.Focus()
DateTimePicker1.Focus()
TextBox5.Focus()
TextBox6.Focus()
TextBox7.Focus()
TextBox8.Focus()
TextBox9.Focus()
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Call koneksi()
cmd = New OleDbCommand("Select* from biodata where Nis='" & TextBox1.Text & "'", CONN)
rd = cmd.ExecuteReader
rd.Read()
Dim edit As String = "update biodata set Nama='" & TextBox2.Text & "',TempatLahir='" & TextBox3.Text & "',TanggalLahir='" & DateTimePicker1.Value & "',Alamat='" & TextBox5.Text & "',Ibu='" & TextBox6.Text & "',Ayah='" & TextBox7.Text & "',Email='" & TextBox8.Text & "',NOHP='" & TextBox9.Text & "' where Nis='" & TextBox1.Text & "'"
cmd = New OleDbCommand(edit, CONN)
cmd.ExecuteNonQuery()
MsgBox("Data Berhasil diubah", MsgBoxStyle.Information)
Call Kosongkan()
Call TampilGrid()
End Sub
Private Sub TextBox1_KeyPress1(ByVal paintsender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
If e.KeyChar = Chr(13) Then
Call koneksi()
cmd = New OleDbCommand("select * from biodata where Nis='" & TextBox1.Text & "'", CONN)
rd = cmd.ExecuteReader
rd.Read()
If Not rd.HasRows Then
TextBox2.Text = ""
TextBox3.Text = ""
TextBox5.Text = ""
TextBox6.Text = ""
TextBox7.Text = ""
TextBox8.Text = ""
TextBox2.Focus()
Else
TextBox2.Text = rd.Item("Nama")
TextBox3.Text = rd.Item("TempatLahir")
DateTimePicker1.Value = rd.Item("TanggalLahir")
TextBox5.Text = rd.Item("Alamat")
TextBox6.Text = rd.Item("Ibu")
TextBox7.Text = rd.Item("Ayah")
TextBox8.Text = rd.Item("Email")
TextBox9.Text = rd.Item("NOHP")
TextBox2.Focus()
End If
End If
End Sub
Private Sub TextBox2_KeyPress1(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox2.KeyPress
If e.KeyChar = Chr(13) Then TextBox3.Focus()
End Sub
Private Sub TextBox3_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox3.KeyPress
If e.KeyChar = Chr(13) Then TextBox5.Focus()
End Sub
Private Sub TextBox5_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox5.KeyPress
If e.KeyChar = Chr(13) Then TextBox6.Focus()
End Sub
Private Sub TextBox6_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox6.KeyPress
If e.KeyChar = Chr(13) Then TextBox7.Focus()
End Sub
Private Sub TextBox7_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox7.KeyPress
If e.KeyChar = Chr(13) Then TextBox8.Focus()
End Sub
Private Sub TextBox8_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox8.KeyPress
If e.KeyChar = Chr(13) Then TextBox9.Focus()
End Sub
Private Sub TextBox9_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox9.KeyPress
If e.KeyChar = Chr(13) Then Button2.Focus()
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim response As MsgBoxResult
response = MsgBox(" Anda Yakin Mau keluar dari Sini", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "Pembuat Novri Rizqullah")
If response = MsgBoxResult.Yes Then
MsgBox("Terima Kasih", MsgBoxStyle.Information, "Pembuat Novri Rizqullah")
Me.Dispose()
ElseIf response = MsgBoxResult.No Then
MsgBox("Selamat Melanjutkan", MsgBoxStyle.Information, "Pembuat Novri Rizqullah")
End If
Exit Sub
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
TextBox1.Text = ""
TextBox2.Text = ""
TextBox3.Text = ""
TextBox5.Text = ""
TextBox6.Text = ""
TextBox7.Text = ""
TextBox8.Text = ""
TextBox9.Text = ""
TextBox1.Focus()
End Sub
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
If TextBox1.Text = "" Then
MsgBox("Nis masih kosong,silahkan diisi dulu")
TextBox1.Focus()
Exit Sub
Else
If MessageBox.Show("Yakin akan dihapus..?", "", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then
Dim hapus As String = "delete * from biodata where Nis='" & TextBox1.Text & "'"
cmd = New OleDbCommand(hapus, CONN)
cmd.ExecuteNonQuery()
MsgBox("data berhasil dihapus", MsgBoxStyle.Information, "information")
Call TampilGrid()
Call Kosongkan()
Else
Call Kosongkan()
End If
End If
End Sub
Private Sub DataGridView1_CellMouseClick(ByVal sender As Object, ByVal e As DataGridViewCellMouseEventArgs) Handles DataGridView1.CellMouseClick
On Error Resume Next
TextBox1.Text = DataGridView1.Rows(e.RowIndex).Cells(0).Value
TextBox2.Text = DataGridView1.Rows(e.RowIndex).Cells(1).Value
TextBox3.Text = DataGridView1.Rows(e.RowIndex).Cells(2).Value
DateTimePicker1.Value = DataGridView1.Rows(e.RowIndex).Cells(3).Value
TextBox5.Text = DataGridView1.Rows(e.RowIndex).Cells(4).Value
TextBox6.Text = DataGridView1.Rows(e.RowIndex).Cells(5).Value
TextBox7.Text = DataGridView1.Rows(e.RowIndex).Cells(6).Value
TextBox8.Text = DataGridView1.Rows(e.RowIndex).Cells(7).Value
TextBox9.Text = DataGridView1.Rows(e.RowIndex).Cells(8).Value
Call carikode()
If rd.HasRows Then
Call KondisiAwal()
Call koneksi()
Call TampilGrid()
End If
End Sub
End Class
Demikianlah "Cara Membuat Windows Form Simpan Edit Hapus",jika ada yang ingin ditanyakan,atau kritikan harap tinggalkan dikolom komentar yah......
Terima Kasih,Sampai Berjumpa dipostingan selanjutnya!!!
















0 comments:
Post a Comment